Chris
Chris

Reputation: 125

How to remove whitespace in PHP response?

I have a php call I am doing, and the response is used in a foreach function to fill a table.

My table cell contains a huge amount of whitespace, which looks like it is coming from the php response.

Below is an extract and when you take a look in the TD elements, you can see the extra space.

Not sure whats causing this, or how to remove it, maybe a trim?

<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>

Below is the PHP code within my html:

<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>

Upvotes: 0

Views: 2891

Answers (3)

Dub Class Dubclass
Dub Class Dubclass

Reputation: 31

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

For just spaces, use str_replace:

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

For all whitespace, use preg_replace:

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

As simba suggests also clean up the PHP that generates this HTML output as well.

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You can use trim() like below:-

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

And so on for others

Also remove extra spaces created by you for indentation

Note:-

@Fred-ii- valuable comment:-

Sidenote: If you also want clean HTML which is good practice when it comes to having to probably debug from source code, is to add \n's.

I.e. : <?php echo trim("$tdrrow[CAMPAIGN_DATA_VALUE]") . "\n";?> - Otherwise, you may get clumps of code in one line.

Upvotes: 5

Justinas
Justinas

Reputation: 43479

Your spaces are created by indentation in HTML.

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

Upvotes: 3

Related Questions