fork download
  1. <?php
  2.  
  3.  
  4. $html = <<<HTML
  5. <html>
  6.   <head>
  7.   <title>MyPage</title>
  8.   <meta charset="UTF-8">
  9.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10.   </head>
  11.   <body>
  12.   <h1>MyPage</h1>
  13.   <h2>{SUBTITLE}</h2>
  14.   <div>{BODY}</div>
  15.   </body>
  16. </html>
  17. HTML;
  18. $dom = new DOMDocument;
  19. $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
  20.  
  21. $xpath = new DOMXPath($dom);
  22. // you need to register the namespace "php" to make it available in the query
  23. $xpath->registerNamespace("php", "http://p...content-available-to-author-only...p.net/xpath");
  24. $xpath->registerPhpFunctions();
  25.  
  26. // add delimiter to your pattern
  27. $regex = '/^{[^}]+}$/';
  28.  
  29. // search your node anywhere in the DOM tree with "//"
  30. $items = $xpath->query("//*[php:functionString('preg_match', '$regex', text())>0]");
  31.  
  32. foreach ($items as $tag) {
  33. $tag->parentNode->removeChild($tag);
  34. }
  35. echo $dom->saveHTML();
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
<html>
    <head>
        <title>MyPage</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>MyPage</h1>
        
        
    </body>
</html>