title: Encode HTML for JSON string
body: <p>I am storing html in a mysql database as raw html. I am pulling the contents and placing it into an array as follows. The array is the json_encoded, but if there is any double quotes or urls then the javascript displaying the JSON string breaks.</p>
  
  <p>Is it possible to encode html to work through JSON?</p>
  
  <p>Here is an extract of what I am using</p>
  
  <pre><code>    &lt;?php
      $rows = array();
      $sql ="SELECT html FROM table";
  
      try {  
          $sth = $dbh-&gt;query($sql); 
          while($r = $sth-&gt;fetch(PDO::FETCH_OBJ)) {
              $rows[] = $r;
          }
      }
      catch(PDOException $e) {  
          echo "I'm sorry, Dave. I'm afraid I can't do that. $e";  
      } 
      ?&gt;
  
  var json = JSON.parse('&lt;?php print json_encode(json_encode($rows)); ?&gt;');
  </code></pre>
  
  <p>The json string currently outputting throws a javascript error saying unexpected &lt;</p>
  
  <pre><code>[{"html":"&lt;a href="http:\/\/www.url.com"&gt;\n&lt;img src="http:\/\/www.url.com\/img\/logo.png"&gt;\n&lt;\/a&gt;"}]
  </code></pre>
  
  <p>Please no lectures on why this may be a bad thing. This is what my client has requested specifically and I just need to know if it is possible.</p>