I have a MySQL table with field contents containing line breaks and my php code inserts those fields in html like this:

<td onmouseover="$row[0]">

The output is something like this:

<td onmouseover="Field
Contents
">


I have tried to use str_replace like this

str_replace("\n","",$row[desc])

and like this

str_replace("
","",$row[desc])

and it doesn't replace anything.

Does anyone know how to remove line breaks from a string?

Thanks,
Chris.

    well you are on the right track with the string replace, what you need to be using is preg_replace, you can read a very nice paper on it at php.net just type in preg_replace at the search bar at the top

      well you are on the right track with the string replace, what you need to be using is preg_replace, you can read a very nice paper on it at php.net just type in preg_replace at the search bar at the top

        preg_replace("\n","",$row[desc])

        it returns this error

        Warning: Empty regular expression in ... on line ...

          try this

          $pat = "\n";
          preg_replace($pat,"",$row[desc]);
          

          that may work better

            thanks, but it is still giving me that error. 🙁

              i wonder if this is even possible.

              i hope so...

                well, DUH, I must still need some more sleep

                this will do the trick for you

                preg_replace("/\n/","",$row[desc]);
                

                  Try this:

                  preg_replace("/\n\r|\n|\r/","",$row[desc])

                  If that doesn't work, it's got to be the "desc"

                  The problem with working with different operating systems is that some of them terminate lines with \n (unix), some with \r (mac), and some with \n\r (windows).

                    i fixed it...

                    thanks for your help guys, although i did it a different way.

                    when the user is submitting the information it loads it into an invisible textbox which automatically removes the line breaks.

                      Write a Reply...