Log in

View Full Version : php ignore the first whitespaces



Dennis_Gull
08-13-2007, 11:33 PM
Hey, I have a text in my mySQL db that look like this:
<whitespace>hello, this is
some text...
And when i try to output it I get this:
Hello, this is
some text..

I already use nl2br for row change but im not sure how to keep the whitespaces before any text appear on the row, is there a function for that or will i have to convert all the whitespace to &nbsp?

edit:
apparently this code also ignore the whitespace in the start of a row :)
<whitespace> is just a space.

Twey
08-13-2007, 11:35 PM
You'll have to convert them.
str_replace(' ', '&nbsp;', $tring);

Dennis_Gull
08-13-2007, 11:38 PM
okay, thanks for the reply.

Twey
08-13-2007, 11:57 PM
Oh, and note that it's HTML that causes the whitespace to be collapsed, not PHP. Always check the source of your page when debugging.

james438
08-14-2007, 12:11 AM
I thought that looked familiar. I'm pretty sure I tried that once before, so I tried it again just now on my testing page with the following code:


<php $text=" t t";
str_replace(' ', '&nbsp;', $text);
echo "$text";
?>

but all I got was "t t" with no space before the first "t" and only one between the two t's. That is why I started using:


<php $text=" t t";
$text=preg_replace('/((\040){2,2})/',"&nbsp;&nbsp;",$text);
echo "$text";
?>

james438
09-03-2007, 03:58 PM
This is a bit late, but I thought it was relevant. In my last post I said that
<php $text=" t t";
str_replace(' ', '&nbsp;', $text);
echo "$text";
?> didn't work. I should have used
<php $text=" t t";
$text=str_replace(' ', '&nbsp;', $text);
echo "$text";
?> Just posting in case anyone doesn't see the error.