Reputation:
I have the following HTML:
<div><p><img src="https://test1.jpg" /></p></div>
<p>aaa</p>
<p>bbb</p>
<p>ccc<div>ddd <img src="http://test2.jpg" /></div></p>
<p>eee</p>
<p>fff</p>
<p>ggg</p>
<p>hhh</p>
<p>iii</p>
<div><p><img src="https://test3.jpg" /></p></div>
But I need to remove the div
tag around the image outside the p
tag; the expected output is:
<p><img src="https://test1.jpg" /></p>
<p>aaa</p>
<p>bbb</p>
<p>ccc<div>ddd <img src="http://test2.jpg" /></div></p>
<p>eee</p>
<p>fff</p>
<p>ggg</p>
<p>hhh</p>
<p>iii</p>
<p><img src="https://test3.jpg" /></p>
Does anybody know how to do it with PHP preg_replace
function?
Upvotes: 0
Views: 302
Reputation: 4880
you really dont want to use regex to do this, you should use DOMDocument instead. Whilst this seems longer and more complicated, its much more secure.
$dom = new DOMDocument();
$html = '<div><p><img src="https://test1.jpg" /></p></div>ccc<div>ddd <img src="http://test2.jpg" /></div>';
libxml_use_internal_errors(true);
$dom->loadHTML($html);
foreach($dom->getElementsByTagName( 'div' ) as $node) {
// this bit is a little hacky, but if you can predict the values use it to exclude some items
if(strpos($node->nodeValue, 'ddd') !== false) {
continue;
}
$fragment = $dom->createDocumentFragment();
while($node->childNodes->length > 0) {
$fragment->appendChild($node->childNodes->item(0));
}
$node->parentNode->replaceChild($fragment,$node);
}
$html = $dom->saveHTML();
echo $html;
Upvotes: 1