#1
  1. No Profile Picture
    php ninja
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Edmonton, Canada
    Posts
    5
    Rep Power
    0

    Line Breaks and Javascript


    I have a php script creating a couple lines of javascript, but I need a way to remove the line breaks.

    Data is being read from a database, which populates a javascript array.. Basically, I need to turn this:

    content[1] = "data from database
    that has line breaks";

    into this:

    content[1] = "data from database \n that has line breaks.

    note the "\n"

    ----

    I tried replacing "\n" with "\\n" to escape the linefeed character, but it displayes "\\n" in the sourcecode, with line breaks.

    Any suggestions?
    Last edited by Lanny; October 5th, 2002 at 10:20 PM.
  2. #2
  3. No Profile Picture
    php ninja
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Edmonton, Canada
    Posts
    5
    Rep Power
    0

    Resolution


    Well, after tearing my hair out, I figured out what I needed to do to fix it.

    For any of those interested, in order to replace a physical line break with a \n character, so javascript still interprets the line break, simply use the following function

    PHP Code:
    function replaceStupidLineBreaks($string) {
      return 
    strtr($string,array("\n"=>"\\\n","\\r"=>"","\\t"=>"","\\0"=>""));

    The function removes all of the linefeeds and breaks, but leaves intact the \n character in the source code.

    Im sure there is probably a nicer way to write this, but this worked nice for me.
    Last edited by Lanny; October 5th, 2002 at 10:20 PM.

IMN logo majestic logo threadwatch logo seochat tools logo