fork download
  1. <?php
  2. $xmlstr = <<<XML
  3. <sections>
  4.   <section>
  5.   <paragraph>
  6.   <literal class="html">
  7.   &lt;img alt="" src="http://a...content-available-to-author-only...e.org/empty.png" /&gt;&lt;/span&gt;&lt;/span&gt; Yes/no&amp;nbsp;&lt;br /&gt;
  8.   &lt;img alt="" src="http://a...content-available-to-author-only...e.org/empty.png" /&gt;&lt;/span&gt;&lt;/span&gt; Other text/no&amp;nbsp;&lt;br /&gt;
  9.   </literal>
  10.   </paragraph>
  11.   </section>
  12. </sections>
  13. XML;
  14.  
  15. $sections = new SimpleXMLElement($xmlstr);
  16.  
  17. foreach ($sections->section->paragraph as $paragraph) {
  18. $re = "~<img.*?>(?=</span)~";
  19. $subst = "<custom name=\"my_checkbox\"></custom>";
  20. $paragraph->literal = preg_replace($re, $subst, $paragraph->literal);
  21. }
  22.  
  23. echo $sections->asXML();
  24.  
  25. ?>
  26.  
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
<?xml version="1.0"?>
<sections>
  <section>
    <paragraph>
      <literal class="html">
        &lt;custom name="my_checkbox"&gt;&lt;/custom&gt;&lt;/span&gt;&lt;/span&gt; Yes/no&amp;nbsp;&lt;br /&gt;
        &lt;custom name="my_checkbox"&gt;&lt;/custom&gt;&lt;/span&gt;&lt;/span&gt; Other text/no&amp;nbsp;&lt;br /&gt;
      </literal>
    </paragraph>
  </section>
</sections>