title: Use PHP to find and replace html element content with a given classname within a document
body: <p>I am working on a light-weight inline content editor for my site. Similar to <a href="http://cushycms.com/" rel="nofollow">CushyCMS</a> or <a href="http://surrealcms.com/" rel="nofollow">SurrealCMS</a>, the 'editable' elements of my page are defined with a <code>class="editable"</code>.</p>
  
  <p>I would like to pass 2 variables to php via AJAX:</p>
  
  <p><code>$page</code> The document to be written to (eg. '/path/index.html')</p>
  
  <p><code>$json</code> A json string: <code>{"0":"First Content","1":"Second Content","2":"Third Content"}</code> where the key is the index of all elements with <code>class="editable"</code> and the value is each element's innerHTML.</p>
  
  <p>In PHP 5.3, how can I open <code>$page</code> and find/replace <code>class="editable"</code> elements?</p>
  
  <pre><code>$page = $_POST['page'];
  $json = $_POST['json'];
  
  $doc = new DOMDocument();
  $doc = DOMDocument::loadHTMLFile($page);
  
  // Detect DOM elements with class="editable"  &amp; replace content with JSON data
  
  // Save changes to designated file
  $doc-&gt;saveHTMLFile($page); // Write to file eg, 'index.html'
  
  echo ('Success');
  </code></pre>