Page 1 of 2

Replace Line Breaks

Posted: Fri Jan 04, 2008 9:04 am
by icesolid
I have a button on my site that when clicked fills in a textarea. Everything works fine unless the content in variable $row["fill"] has a line break. Then I get an JavaScript error.

Example:

This is test. (works)

This does not work:

This is text.

This is more text.


My current code:

Code: Select all

onclick="document.form.field55.value = '<?php echo htmlentities($row5["fill"]); ?>';">
Is there anyway of escaping this line break?

Posted: Fri Jan 04, 2008 9:06 am
by s.dot
A line break should show up fine inside of a textarea. That's not the problem, I wouldn't think.

Posted: Fri Jan 04, 2008 9:07 am
by icesolid
Its the line break being inside of the JavaScript code, that is causing my error.

Posted: Fri Jan 04, 2008 10:37 am
by goldberg
Do you mean ?

Code: Select all

onclick="document.form.field55.value = '<?php echo htmlentities($row5["text<BR>text"]); ?>';">

Posted: Fri Jan 04, 2008 10:40 am
by icesolid
no

Posted: Fri Jan 04, 2008 12:16 pm
by s.dot
Oh, I see what you're saying. you could str_replace() the line breaks (PHP_EOL)

Posted: Fri Jan 04, 2008 2:07 pm
by icesolid
Still not sure?

Posted: Fri Jan 04, 2008 5:06 pm
by alex.barylski
Are you sure it's a line break causing the error?

Typically these problems are caused when your outputted PHP code contains a single or double quote...have you tried escaping the string before echo'ing it to the JS string ?

Posted: Fri Jan 04, 2008 9:14 pm
by Jonah Bron
I think the solution you are searching for is below:

Code: Select all

$output = str_replace('
', '\\r\\n', $output);
echo $output;
This way all line breaks are replaced by \r\n, which is recognized by javascript as a line break.

Posted: Sat Jan 05, 2008 7:23 am
by icesolid
Still not working. Nothing changes using the code above.

Posted: Sat Jan 05, 2008 11:28 am
by Jonah Bron
maybe

Code: Select all

$output = str_replace('
', stripslashes('\\r\\n'), $output);
echo $output;
If that doesn't work, view the source, and see what is being echoed: line breaks, or something else?

Posted: Sun Jan 06, 2008 5:26 am
by Popcorn
It looks like the following is the cause....

http://en.wikipedia.org/wiki/Javascript_syntax:
Unlike C, whitespace in JavaScript source can directly impact semantics. Because of a technique called "semicolon insertion", any statement that is well formed when a newline is parsed will be considered complete (as if a semicolon were inserted just prior to the newline).
When I encountered it in variable assignment I got the same weirdness. The following code (maintaining whitespace):

Code: Select all

itemsinfo["descr"][384]="White Single Cuff Ring
";
gives you:

Code: Select all

Syntax error while loading: line 1165 of inline script at ... : 
itemsinfo["descr"][384]="White Single Cuff Ring
------------------------------------------------^
because the value had a newline in it.

As suggested, stripping out newline chars (cater for all OSs) should fix the problem.

Posted: Tue Jan 08, 2008 8:14 am
by icesolid
I basically want to do this.

If there are any new lines, replace them with \n\n

Code: Select all

<?php
str_replace("

", "\n\n", $output);
?>
Somehow make that work, because that code does not, but it is the idea.

Posted: Tue Jan 08, 2008 10:43 am
by pickle
What about escaping the newline:

Code: Select all

str_replace("\n","\\n",$output)

You may need another '\' in the replacement string - untested.

Posted: Wed Jan 09, 2008 8:02 am
by icesolid
Anyone curious about my solution, here it is.

After tinkering around for awhile this is what I came up with.

Code: Select all

str_replace("\n", "\\n", htmlentities(str_replace("\r", "\\r", $row5["fill"])))
The \r \\r replace is required, otherwise there are still breaks in the output.