I am literally about to start tearing my hair out...

I'm building a site that allows users to post ads. These ads will be stored in a MySQL database so obviously I'm passing the values through mysql_real_escape_string before storing them BUT, of course, the main description may have line breaks in it that I want to preserve so when displaying
the ad, I use nl2br. Sounds simple, right? Except it's not working, it's as thought nl2br is just ignoring the \r etc.

At the moment I'm working on the ad preview so my description is coming into the preview page via a session variable $_SESSION['m_desc'] and all I'm putting is

echo nl2br($_SESSION['m_desc']);

(inside php tags, of course). But the string just comes out with the \r\n s still in there.

I've shown the $_SESSION variables using print_r and m_desc looks like this:

Array
(
[m_desc] => Line One\r\nLine Two\r\nLine Three
)

and that's exactly how it's shown on screen AFTER nl2br. To really make me scream, if I replace the session variable with a test one, e.g.

$teststr="Line One\r\nLine Two\r\nLine Three";
echo nl2br($teststr);

it works perfectly! It's as though the session variable isn't really a string but I'm completely stumped.

Any help greatly appreciated!

Jon

    try this:

    $string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
    

    Editted

      I've been using this function that does what you need:

      // preserving line breaks in text blocks
      function nl2br_indent($string, $indent = 0)
      {
         //remove carriage returns
         $string = str_replace("\r", '', $string);
      
         //convert indent to whitespaces if it is a integer.
         if (is_int($indent)) {
             //set indent to length of the string
             $indent = str_repeat(' ', (int)$indent);
         }
      
         //replace newlines with "<br />\n$indent"
         $string = str_replace("\n", "<br />\n".$indent, $string);
         //add  the indent to the first line too
         $string = $indent.$string;
      
         return $string;
      }
      

      Hope this helps.

        Thanks Scrupul0us and ZZZ, it's now solved and I'm able to start patching my hair back on.

        In actual fact I needed to use TWO \ as the key to replace so my line looks like

        $string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);

        which to me looks like I'm having to 'escape' the escape character. I'm not entirely (well, at all, actually) certain why that should be as it seems to contradict the blurb of my bible here.

        Anyway, it's working perfectly now so that's the main thing, if anyone's able to tell me why I'd be interested...

        Thanks again guys, it's another task completed..

        Jon

          yea makes sense to have to escape it... \n is literal, \n is string

            4 years later

            The reason you had double slashes is because the characters were escaped.

            So if you take your var and do:

            nl2br(stripslashes($var)) then you wont need to do the replace and you can go back to using nl2br

            eli geske

              Write a Reply...