How to escape new line chars in php to output into a javascript string value?
<p>I pulling out some html/text strings that I need to insert into a javascript variable.</p>

<p>eg, that's how it would look in php: </p>

<pre><code>echo "&lt;script type=\"text/javascript\"&gt;\n";
echo "var myvar='{$value}'";
echo "\n&lt;/script&gt;";
</code></pre>

<p>The problem with the above approach is that some special characters would actually break the javascript code.</p>

<p>So, I tried using htmlspecialchars:</p>

<pre><code>htmlspecialchars($value,11,'utf-8',true); //11 stands for ENT_QUOTES|ENT_SUBSTITUTE
</code></pre>

<p>This did replace some unusual chars and most importantly the quotes.</p>

<p>However the new line chars pass it by and break my javascript.</p>

<p>So how could I escape the new line chars? I need to preserve them to be used later in the textareas.</p>

<p>*<strong><em>EDIT</em>*</strong> I will post a sample value of my variable. (They are actually the input from Tiny_mce)</p>

<pre><code> &lt;p&gt;You've been...&lt;/p&gt;
 &lt;p&gt;&lt;iframe src="http://www.youtube.com/embed/8d7OBluielc?wmode=transparent" frameborder="0" width="640" height="360"&gt;&lt;/iframe&gt;&lt;/p&gt;
</code></pre>
