Results 1 to 6 of 6

Thread: php ignore the first whitespaces

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default php ignore the first whitespaces

    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.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You'll have to convert them.
    Code:
    str_replace(' ', '&nbsp;', $tring);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    okay, thanks for the reply.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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 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 Code:
    <php $text="          t            t";
    $text=preg_replace('/((\040){2,2})/',"&nbsp;&nbsp;",$text);
    echo 
    "$text";
    ?> 

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •