2017-03-08 34 views
0

我有一个PHP调用,我正在做,并在foreach函数中使用响应来填充表。如何删除PHP响应中的空白?

我的表格单元格包含大量的空白,它看起来像来自于PHP响应。

下面是一个摘录,当您查看TD元素时,可以看到额外的空间。

不知道是什么原因造成这种情况,或者如何删除它,也许修剪?

<div class="container-fluid" style="margin-top:2em;"> 
      <div class="row"> 
       <div class="col-lg-12" style="background: none;"> 
        <h4>Test Drives</h4> 
             <table class="table table-fixed table-sm table-hover" style="height: 100px; background:pink;"> 
         <thead> 
          <tr> 
          <th> 
           Value 
          </th> 
          <th> 
           Date Submitted 
          </th> 
          <th> 
           User submitted 
          </th> 
           <th> 
           Market 
          </th> 
           <th> 
           Notes 
          </th> 
           <th> 
           Last Update by 
          </th> 
          </tr> 
         </thead> 
         <tbody> 
                <tr> 
           <td> 
            2        </td> 
           <td> 
            2017-03-08        </td> 
           <td> 
            ADMIN        </td> 
           <td> 
            DE        </td> 
           <td> 
            This is a test        </td> 
           <td> 
            ADMIN        </td> 
          </tr> 
                </tbody> 
        </table> 
       </div> 
      </div> 
     </div> 

下面是我的HTML中的PHP代码:

<div class="container-fluid" style="margin-top:2em;"> 
      <div class="row"> 
       <div class="col-lg-12" style="background: none;"> 
        <h4>Test Drives</h4> 
        <?php include 'campaign_detail.inc.php'; ?> 
        <table class="table table-fixed table-sm table-hover" style="height: 100px; background:pink;"> 
         <thead> 
          <tr> 
          <th> 
           Value 
          </th> 
          <th> 
           Date Submitted 
          </th> 
          <th> 
           User submitted 
          </th> 
           <th> 
           Market 
          </th> 
           <th> 
           Notes 
          </th> 
           <th> 
           Last Update by 
          </th> 
          </tr> 
         </thead> 
         <tbody> 
         <?php foreach($testdrives as $tdrrow) {?> 
          <tr> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_VALUE]";?> 
           </td> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_DATE_CREATED]";?> 
           </td> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_USER_CREATED]";?> 
           </td> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_MARKET]";?> 
           </td> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_NOTES]";?> 
           </td> 
           <td> 
           <?php echo "$tdrrow[CAMPAIGN_DATA_USER_UPDATED]";?> 
           </td> 
          </tr> 
          <?php } ?> 
         </tbody> 
        </table> 
       </div> 
      </div> 
     </div> 
+0

你能向我们展示产生HTML的示例代码吗? – JustBaron

+0

我在我的代码片段中的html中添加了PHP代码。 – Chris

+1

问题是,你已经在PHP回声之前放了很多空白,然后在关闭之前再次在下一行标记 –

回答

5

您可以使用trim()象下面这样: -

<?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]");?> 

等了别人

还要去掉多余的您创建的空间为缩进

注: -

@弗雷德-II-宝贵的意见: -

旁注:如果您还想要干净的HTML,这是很好的做法,当谈到有可能给调试源代码,请添加\n's

I.e. :<?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]") . "\n";?> - 否则,您可能会在一行中获得大量代码。

+0

它是.php,但最初的问题是我使用的额外缩进。 – Chris

+0

@Chris是的,我检查了它并在我的答案中加入了 –

+1

@Chris Sidenote:如果您还想要干净的HTML,这对于必须从源代码进行调试是一种很好的做法,那就是添加'\ n''s。 I.e .:'<?php echo trim(“$ tdrrow [CAMPAIGN_DATA_VALUE]”)。 “\ n”;?>' - 否则,您可能会在一行中看到很多代码。 –

3

您的空间是通过HTML中的缩进创建的。

使用<td><?= trim($tdrrow[CAMPAIGN_DATA_VALUE]);?></td>

+0

PHP短回声 - “<?=”从PHP 5.4及更高版本启用 –

+0

这基本上是问题所在。由于我做了一些编码步骤,我添加了不适当的额外换行符。 – Chris

1

https://stackoverflow.com/a/2109339/7403455

复制对于刚刚空间,使用str_replace函数:

$string = str_replace(' ', '', $string); 

对于所有的空格,使用了preg_replace:

$string = preg_replace('/\s+/', '', $string); 

随着辛巴也表明干净生成这个H的PHP TML输出。