views:

70

answers:

4

Ok, so basically I am using the fckeditor as a way for my user to enter some information. When it is stored in the DB it is stored like so,

<p> </p>
<div style=\"margin: 0in 0in 10pt\"><b><span style=\"color: #666666\">Test Title</span></b><span style=\"color: #666666\"> <br />
Address goes here <br />
This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location.</span></div>

So anyway, when I output this text it is sent into a function that takes it and creates a pinpoint on a google map and that text goes inside of the popup window. The problem I am running into is that when the php outputs the javascript function call it looks like this,

showAddress(" <p> </p>
<div style=\"margin: 0in 0in 10pt\"><b><span style=\"color: #666666\">Test Title</span></b><span style=\"color: #666666\"> <br />
Address goes here <br />
This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location. This is some lengthy text about the location.</span></div>");

The space after the line breaks and basically whenever you hit enter is causing the javascript to exit with an 'unterminated string literal'.

Thanks,
Levi

+2  A: 

$value = str_replace(array("\n", "\r"), "", $string);

Updated because Andrew Moore thought of the \r key. But, you don't need to use an array in the replace argument.

This will replace all newlines with nothingness.

Chacha102
Oh wow, I didn't really make the connection that a newline was being inserted. Nor would I have thought you could replace newline characters that are made in javascript with php.
Levi
+2  A: 

Use str_replace() to remove new lines:

str_replace(array("\n", "\r"), '', $someString);

Also, since you are storing text, you would be better to use the TEXT data type versus the BLOB as the TEXT is locale-aware.

Andrew Moore
+1  A: 

I think you'd want to leave a space and not and empty string, so the text doesn't just run together.

And being of perl heritage, I'd use

$cleanedString = preg_replace( "/[\n\r]+/", " ", $stringWithReturns );

This will catch multiple returns and replace with a single space.

Devin Ceartas
+1  A: 

You should be using json_encode to make sure your HTML goes into javascript correctly:

<script>
showAddress(<?php echo json_encode($htmlFromDB); ?>);
</script>

Newlines shouldn't be a problem any more.

too much php
Nice, PHP thinks of everything. I note the php.net site says: "PHP 5 >= 5.2.0" so it's only available in fairly recent PHP installs, I guess
Devin Ceartas