function cleanit($string)
{


$string=str_replace("$","\$",$string);
$string=strip_tags($string);
$string=str_replace("\n\n","<p>",$string);
$string=str_replace("\n","<br>",$string);

return $string;



}

I have the above function to clean up user added text in a messageboard I'm writing. My problem is the line that replaces 2 newlines with a <p> doesn't work and it's throwing my formatting off. Can anyone tell me why?

Thanks!:p

    In stead of using the string replace function to get rid of the newline characters, why not just use trim();. Seems like it would be much easier...

    take a look here:
    http://www.php.net/manual/en/function.trim.php

    It should be able to handle exactly what you're working on.

      oops, sorry...didn't realize you were trying to replace the /n's with <p>...trim would just remove the /n

        Instead of using "str_replace", try using "ereg_replace", and instead of replacing "\n\n", try replacing "\r\n".

        Basically, try replacing the line that says "$string=str_replace("\n\n","<p>",$string);
        with something like:
        "$string=ereg_replace("\r\n", "<p>", $string);"
        and see how well that works.

        Further, if you are wanting to trim off the whitespace (which is pretty common), you can do pretty much everything on one line, like so:

        $string = strip_tags(trim(ereg_replace("\r\n", "<p>", $string)));

        Hope this helps.

        -9mm-

          Originally posted by issue9mm
          Instead of using "str_replace", try using "ereg_replace", and instead of replacing "\n\n", try replacing "\r\n".

          Basically, try replacing the line that says "$string=str_replace("\n\n","<p>",$string);
          with something like:
          "$string=ereg_replace("\r\n", "<p>", $string);"
          and see how well that works.

          Further, if you are wanting to trim off the whitespace (which is pretty common), you can do pretty much everything on one line, like so:

          $string = strip_tags(trim(ereg_replace("\r\n", "<p>", $string)));

          Hope this helps.

          -9mm-

          I aggree with issue9mm

          using

          $string=ereg_replace("\r\n", "<p>", $string);

          will work great, though Id use <br> not <p>

          $string=ereg_replace("\n", "<br>", $string);

            Originally posted by DigitalExpl0it


            I aggree with issue9mm

            using

            $string=ereg_replace("\r\n", "<p>", $string);

            will work great, though Id use <br> not <p>

            $string=ereg_replace("\n", "<br>", $string);

            The problem is that it would make absolutely no difference beyond slowing the script down.

            My suggestion would be to actually read what the original problem is.

            I'll guess that there may be stray \r characters in there between the double \n pair. In that case:

            str_replace("\r\n\r\n","<p>",$string);
            

            would be the fix.

              Write a Reply...