如何删除PHP响应中的空格?

Chr*_*ris 0 html php

我正在进行一个php调用,并且响应在foreach函数中用于填充表.

我的表格单元格包含大量的空白,看起来它来自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>
Run Code Online (Sandbox Code Playgroud)

下面是我的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>
Run Code Online (Sandbox Code Playgroud)

Ana*_*Die 5

您可以使用trim()如下: -

<?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]");?> 
Run Code Online (Sandbox Code Playgroud)

对其他人来说等等

同时删除由您创建的额外空格以进行缩进

注意:-

@ Fred-ii-有价值的评论: -

旁注:如果您还想要干净的HTML,那么在必须从源代码调试时这是一个很好的做法,就是添加\n's.

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