I am trying to replace the HTML5 elements with a <div
tag for older browsers.
Code I am using is as follows. I simplified for easy understanding:
<article class="article">article - Lorem ipsum dolor sit amet</article>
<pre>
<article class="article-pre">article - Lorem ipsum dolor sit amet</article>
<summary class="summary-pre">summary - Lorem ipsum dolor sit amet</summary>
</pre>
<code>
<article class="article-pre">Lorem ipsum dolor sit amet</article>
<summary class="summary-pre">Lorem ipsum dolor sit amet</summary>
</code>
I tried doing some experiments but failed. I used the code as follows:
$dom = new DOMDocument;
$dom->loadHTML($content);
$xp = new DOMXPath($dom);
$nodes = $xp->query('//text()[not(ancestor::pre) and contains(.,' . $element . ')]');
foreach($nodes as $node) {
$node->nodeValue = str_replace($element, $replace, $node->nodeValue);
}
echo $dom->saveXML($dom->documentElement);
I want this thing to work on JavaScript disabled browsers that do not support HTML5 elements, hence using any JavaScript is not an option.
If there is any other better way to do this using
php
then please suggest.