Replace Line Breaks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Replace Line Breaks

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

A line break should show up fine inside of a textarea. That's not the problem, I wouldn't think.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Its the line break being inside of the JavaScript code, that is causing my error.
goldberg
Forum Newbie
Posts: 3
Joined: Fri Jan 04, 2008 8:15 am
Location: Baltimore, MD

Post by goldberg »

Do you mean ?

Code: Select all

onclick="document.form.field55.value = '<?php echo htmlentities($row5["text<BR>text"]); ?>';">
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

no
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Oh, I see what you're saying. you could str_replace() the line breaks (PHP_EOL)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Still not sure?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 ?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

Still not working. Nothing changes using the code above.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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?
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Post 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.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What about escaping the newline:

Code: Select all

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

You may need another '\' in the replacement string - untested.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post 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.
Post Reply