fork(1) download
  1. <?php
  2. $html = <<< EOF
  3. <p>Commodity Exchange on 5 April 2016 settled as following graph:</p>
  4. <p><img alt=\"\" src=\"ckeditor/plugins/imageuploader/uploads/986dfdea.png\"
  5. style=\"height:163px; width:650px\" /></p></p>
  6. <p>end of string</p>
  7. EOF;
  8.  
  9.  
  10. /*
  11. $doc = new DOMDocument();
  12. $doc->loadHTML($html);
  13. $doc->preserveWhiteSpace = false;
  14. // all links in document
  15.  
  16. $arr = $doc->getElementsByTagName("div"); // DOMNodeList Object
  17. foreach($arr as $item) { // DOMElement Object
  18. $div = preg_replace('/\s+/im', '', $item->textContent);
  19.  
  20. //echo $item->textContent."\n";
  21. if(empty($div)){
  22. $link = $doc->createElement('br');
  23. $doc->parentNode->replaceChild($link, $item);
  24. }
  25. }
  26.  
  27. echo $doc->saveHTML();
  28. */
  29. $html = tidy_repair_string($html,array(
  30. 'output-html' => true,
  31. 'wrap' => 80,
  32. 'show-body-only' => true,
  33. 'clean' => true,
  34. 'input-encoding' => 'utf8',
  35. 'output-encoding' => 'utf8',
  36. ));
  37.  
  38.  
  39. $dom = new DOMDocument();
  40. $dom->loadHtml($html);
  41.  
  42.  
  43.  
  44. $x = new DOMXpath($dom);
  45. foreach($x->query('//p/img') as $pImg){
  46. //get image name
  47. $imgFileName = basename(str_replace('"', "", $pImg->getAttribute("src")));
  48. $replace = $dom->createTextNode("#$imgFileName");
  49. $pImg->parentNode->replaceChild($replace, $pImg);
  50. # loadHTML causes a !DOCTYPE tag to be added, so remove it:
  51. $dom->removeChild($dom->firstChild);
  52. # it also wraps the code in <html><body></body></html>, so remove that:
  53. $dom->replaceChild($dom->firstChild->firstChild, $dom->firstChild);
  54. echo str_replace(array("<body>", "</body>"), "", $dom->saveHTML());
  55.  
  56. }
  57.  
Success #stdin #stdout 0.02s 52504KB
stdin
Standard input is empty
stdout
<p>Commodity Exchange on 5 April 2016 settled as following graph:</p>
<p>#986dfdea.png</p>
<p>end of string</p>