Where are my spaces going?

I have a form with $_POST[‘firstName’]

Why is it that if I type in the field “<space>Debbie<15spaces>” and then echo the field value with PHP, I only see…

echo "untrimmed: firstName='" . $_POST['firstName'] . "'<br />";

’ Debbie ’

Sincerely,

Debbie

html collapses white space

if you need to see them, use the PRE tag

I was playing around with this PHP code to understand how it works and can maybe make my code simpler…


// Trim all form data.
$trimmed = array_map('trim', $_POST);

echo "untrimmed: firstName='" . $_POST['firstName'] . "'<br />";
echo "trimmed: firstName='" . $trimmed['firstName'] . "'<br />";

I figured if I entered a whole bunches of spaces around my name in the form it would help demonstrate that it was working.

Debbie

Non-breaking spaces don’t collapse.

cheers,

gary

You want simpler, STOP USING DOUBLE QUOTES ON PHP STRINGS! :rofl:

… and you can get the spaces to show with a hint of CSS without the container going block-level using white-space: pre, though really PRE by itself should be enough to do what you want.


echo '
<pre>
untrimmed: firstName = "',$_POST['firstName'],'"
trimmed: firstName = "',$trimmed['firstName'],'"
</pre>';

Using single quotes also means white space in the output is preserved, and it only needs ONE echo statement… and the presence of <pre> means your carriage returns show up on the page.

Okay, thanks.

Debbie