im using php and i wanna know how to delete when <p><a><img></a></p>
from this:
<p>
<strong>Lorem</strong>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor
<a href="xxxx" target="yyy">sit amet
</a>, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
<img alt="" height="xxx" src="yyy" width="zzz" />
</p>
<p>
<a href="xxx" target="_blank">
<img alt="" height="xxx" src="yyy" width="zzz" />
</a>
</p>
to this:
<p>
<strong>Lorem</strong>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor
<a href="xxxx" target="yyy">sit amet
</a>, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
<img alt="" height="xxx" src="yyy" width="zzz" />
</p>
<a href="xxx" target="_blank">
<img alt="" height="xxx" src="yyy" width="zzz" />
</a>
i just wanna delete tag <p></p>
when <p><a><img></a></p>
i've tried using preg_replace(); function but i cant get it could you please help me, thanks
<?php
$html = <<< EOFHTML
<p>
<strong>Lorem</strong>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor
<a href="xxxx" target="yyy">sit amet
</a>, consectetur adipisicing elit, sed do eiusmod
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
<img alt="" height="xxx" src="yyy" width="zzz" />
</p>
<p class="foo">
<a href="xxx" target="_blank">
<img alt="" height="xxx" src="yyy" width="zzz" />
</a>
</p>
EOFHTML;
$html = preg_replace("'(<p[^>]*>)([^<]*<a[^>]*>[^<]*<img[^>]*>[^<]*</a>[^<]*)(</p>)'sim", "$2", $html);
echo $html;
Hi You can use this but note that it will only work when you have <p></p>
with no class
or i
d or any other attribute
like <p class="myclass">
(not work for this).
<?php
$text ='<p>
<a href="http://xxx" target="_blank">
<img alt="xxx" height="xxx" src="xxx" width="xx" />
</a>
</p>';
$text=str_ireplace('<p>','',$text);
$text=str_ireplace('</p>','',$text);
echo $text;
?>
`str_ireplace` `function` returns a string or an array with all occurrences of search in subject (ignoring case) replaced with the given replace value.
for more info read http://php.net/manual/en/function.str-ireplace.php
PHP 5+ comes with a built in DOM Parser. PHP DOMDocument
It can be used to find, remove, and add elements.