Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts

    How can I convert line feeds and carriage returns to <br /> tags

    Hey,

    I need to convert the line feeds and carriage returns to <br /> tags.

    When I tried using RegEx and did it like this:
    PHP Code:
    function strFormat($str)
    {
    return 
    preg_replace('/([\r\n]*)/','<br />',$str);

    It made this word: 'word' to:
    'w
    o
    r
    d'

    And it kind messed the whole page.

    What should I do?

    Thanks.

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Dave .... HostMonster for all of your hosting needs

  • #3
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by djm0219 View Post
    Yes, That's what I was looking for. But is there a way to do this with mysql_real_escape_string?

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Not at all sure what you're asking. They aren't directly related at all. You may certainly use them together to build up a variable for insertion into a database.

    PHP Code:
    $myvar mysql_real_escape_string(nl2br($myvar)); 
    Dave .... HostMonster for all of your hosting needs

  • #5
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    238
    Thanks
    18
    Thanked 34 Times in 33 Posts
    Quote Originally Posted by BarrMan View Post
    It made this word: 'word' to:
    'w
    o
    r
    d'
    There is a solution already posted but I would like to notice what error you had with reg. exp. because later you can come accross with it again So you need to change * with + because * means zero or more matches while + means one or more... in other words after each char it matches zero new lines or carriage returns and inserts <br>


  •  

    Posting Permissions

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