How to echo php string safely in a java script onclick identifier
<p>Please don't mark this as duplicate as I can't find a working solution on the very similar questions. None of them seem to fix my specific string.</p>

<p>I have this PHP variable...</p>

<pre><code>&lt;?php

   $og_description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus hendrerit cursus mi, Nullam condi\'mentum posuere odio sed cursus.. Proin posue\'re fermentum, ligula vel lacinia sollicitudin, libero quam ultrices quam, quis tristique orci... enim a justo!'

?&gt;
</code></pre>

<p>The string above has exactly the same special characters I am using.</p>

<p>I am then trying to echo it into this onclick identifier. Obviously <code>&lt;?php echo $og_description; ?&gt;</code> is not going to work on its own.</p>

<pre><code>&lt;a href="#" onclick="fb_publish('&lt;?php echo $fb_app_url; ?&gt;','&lt;?php echo $og_title; ?&gt;','images/icon.png','&lt;?php echo $og_description; ?&gt;'); return false;"&gt;Post to wall&lt;/a&gt;
</code></pre>

<p>I have tried these...</p>

<pre><code>echo htmlspecialchars($og_description);
echo json_encode($og_description);
</code></pre>

<p>and a few others but nothing seemed to fix my string into a format that would not break the identifier array.</p>

<p>Does anyone know how I can fix this?</p>

<p>Thanks
Josh</p>
