For a YouTube web-app I'm building in PHP, I have the simple video player, embedded with an <iframe>
, and then a <div>
with information about the currently loaded video (description, id, title, etc).
A <ul>
contains a list of videos which are fetched using the PHP gData API from YouTube, and each <li>
contains a link which activates JavaScript to change the video player to the correct video and also update the video info on the page.
Here's the issue: gData
returns a multi-line, non-escaped sequence for the video description, which doesn't work in JavaScript. How should I remove line breaks and replace them with <br>
(note that they aren't line breaks like \n
, they are actual line breaks and newlines).
I also have to escape other things that won't work in a JavaScript string, such as the apostrophe character '
. What is the best way to do this?
For a YouTube web-app I'm building in PHP, I have the simple video player, embedded with an <iframe>
, and then a <div>
with information about the currently loaded video (description, id, title, etc).
A <ul>
contains a list of videos which are fetched using the PHP gData API from YouTube, and each <li>
contains a link which activates JavaScript to change the video player to the correct video and also update the video info on the page.
Here's the issue: gData
returns a multi-line, non-escaped sequence for the video description, which doesn't work in JavaScript. How should I remove line breaks and replace them with <br>
(note that they aren't line breaks like \n
, they are actual line breaks and newlines).
I also have to escape other things that won't work in a JavaScript string, such as the apostrophe character '
. What is the best way to do this?
- 10 Looks like you removed all the newlines from your question too... – JJJ Commented Oct 3, 2011 at 19:06
- 3 nl2br() see manual for useage – user557846 Commented Oct 3, 2011 at 19:09
- 2 Provide an example of the returned data that you're dealing with. – Alex Commented Oct 3, 2011 at 19:10
-
1
actual line breaks and newlines
are\n
(and maybe\r
too).won't work in a JavaScript string
? Apostrophes work just fine inside double quoted strings. – gen_Eric Commented Oct 3, 2011 at 19:20 -
1
Sorry about the missing paragraphs, I didn't think about it. I'll correct it next time. @Rocket: You're right, I wasn't thinking. @Dagon: I tried that, it didn't actually remove newlines, just added
<br>
. @mathieu: The data is from PHP, but gets used later in a javascript function. @Alex: I will, as soon as I try something and clean the code a bit. – ahouse101 Commented Oct 3, 2011 at 20:01
2 Answers
Reset to default 6Don't bother trying to escape stuff yourself. Just use json_encode, which'll handle all those details for you:
<script type="text/javascript">
var description = <?php echo json_encode($description) ?>;
</script>
Marc B has given the best answer. Use json_encode: http://php/manual/en/function.json-encode.php Go, upvote his answer.
The following is my original response:
<?php
$data = "Hello, 'world'.\nHow are you doing?\n\"Good?\"\n";
$data = str_replace("\n", '<br>', $data);
$data = str_replace('"', '\"', $data);
$data = str_replace("'", "\'", $data);
echo $data;
?>
The same stuff using regex:
<?php
$data = "Hello, 'world'.\nHow are you doing?\n\"Good?\"\n";
$data = preg_replace("/\n/", '<br>', $data);
$data = preg_replace("/\"|'/", '\"', $data);
echo $data;
?>
Having given those examples, you don't really need to escape both single-quotes and double-quotes. In JavaScript, you can use double-quoted strings as well as single-quoted strings. So, use one and escape the other.
You might also want to escape backslash (replace \
with \\
) to make sure that some funny YouTube uploader doesn't try to break your PHP script by placing a foo\'bar
in the video description. Now, that can break your script if you don't escape backslash because the JavaScript string after replacements would now look like: 'foo\\'bar'
which is a syntax error because the string finishes at 'foo\\'
.