stevenyenzer stevenyenzer - 3 months ago 8
HTML Question

Outputting escaped HTML with PHP includes 4 leading spaces

I'm building a form that produces basic HTML and other plain text output that users will then paste into a content management system.

I set the variables like this:

$date1 = $_POST['date1'];
$city1 = $_POST['city1'];


Then use an if-else statement to set a new variable with that will contain a chunk of the code:

if (!empty($date1) && empty($date2)) {
$emaileventsectionPT = '' . $date1 . ' in ' . $city1 . '';
else (!empty($date2) && empty($date3)) {
$email1eventsectionPT = '' . $date1 . ' in ' . $city1 . '
' . $date2 . ' in ' . $city2 . '';
}


Then echo that variable within the code. In this case, it's in a
textarea all by itself on the output page:

<textarea id="txtfld12" onClick="SelectAll('txtfld12');" cols="75" rows="10">
<?php
echo '' . $emaileventsectionPT . '';
?>
</textarea>


But that and every other textarea on the output page starts with four spaces:

EVENT DATE 1 in EVENT CITY 1
EVENT DATE 2 in EVENT CITY 2


Which means that when I copy and paste the code (or whatever else), I have to remove those spaces every time.

I can't figure out what is causing those spaces and how to get rid of them.

Answer

In your textarea code you have at least 4 newlines. This accounts for the white space you see.

Try this: render the entire block on a single line with no line breaks.

That will remove most if not all of the white space.

You will loose code readability but gain cleaner rendering of the data.