#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    10
    Rep Power
    0
    I have a VARCHAR(255) field in a MySQL database that contains Carriage Returns and Line Feeds.

    I'm trying to display the contents of this field in html with printf, but the CrLf's are being ignored by the printf.

    Does anyone know an easy way of parsing a string with CrLf's into HTML?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    $string = nl2br($string);
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    10
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freddydoesphp:
    $string = nl2br($string);[/quote]

    Thanks Freddy. You got me going in the right direction.

    Here is a much more in depth discussion of nl2br. http://www.php.net/manual/function.nl2br.php

    Thanks again.

  6. #4
  7. No Profile Picture
    Robert_J_Sherman
    Guest
    Devshed Newbie (0 - 499 posts)
    The nl2br() function is certainly one solution, but I've also read a post or two
    where people indicated it could sometime be
    a bit buggy, how true that is I don't know.

    I typically use the following instead of the nl2br() function.

    $mydata = eregi_replace("n","<br>", $mydata);

    This seems to work wonderfully, and to date I've had no problems with it.

    I suppose it's the long form of the nl2br().

    ------------------
    SnR Graphics,
    Low Cost Hosting and Web Development.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    10
    Rep Power
    0

    $mydata = eregi_replace("n","<br>", $mydata);

    This worked like a champ! Thanks. I'm not sure about the stability of nl2br(), but i do know that the "long way" work great.

    Thanks
    Again.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    35
    Rep Power
    15
    Use <pre></pre>:

    echo "<pre>";
    echo $your_varible;
    echo "</pre>";

    ------------------
    Good Good Study
    Day Day Up

    [This message has been edited by xiaoguiwxd (edited September 28, 2000).]
  12. #7
  13. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,643
    Rep Power
    4492
    The only flaky thing about how nl2br() acts is that it doesn't actually remove the newlines. it just addes in the <BR> and leaves the n there too. It comes out looking the same as the regex example on the browser, but different in the source view, because one has <BR>n, the other has just <BR>

    My $0.02.

    John Holmes www.SepodatiCreations.com
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    16
    You guys do realize that you have to do it like this:
    $var=nl2br($var);
    don't you?

    Just:
    nl2br($var);
    wont do anything.

    I don't use that function thought, I use the regex expression used above (but i still keep the n in for clean html).
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    16
    Oh, and the <pre></pre> solution is really a lazy solution to the problem ;p. That is really something nobody should do.

Similar Threads

  1. how do I combine data from 3 html fields.
    By Marv100 in forum PHP Development
    Replies: 1
    Last Post: February 16th, 2004, 12:22 PM
  2. How to get client email??Help plz
    By Gecko22 in forum ASP Programming
    Replies: 12
    Last Post: January 22nd, 2004, 01:40 PM
  3. Build HTML tables with multiple mySQL queries
    By SailFan in forum PHP Development
    Replies: 5
    Last Post: January 19th, 2004, 11:25 AM
  4. not returning records when it should
    By aliaga61 in forum ASP Programming
    Replies: 3
    Last Post: January 16th, 2004, 10:57 AM
  5. 'Object required' error
    By aliaga61 in forum ASP Programming
    Replies: 1
    Last Post: January 16th, 2004, 08:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo