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
, 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?