Strip_tags - split <a> tags with <img>

Can we strip_tags remove tags that are associated with the image?

For example, I want

<a href=""><img src="" /></a>

to be

<img src="" />

I need to save the image and remove all the tags that are around them. Besides using Simple HTML Dom , I was hoping there was an easier way to do this.

+3
source share
2 answers

You cannot do this with strip_tags. You can use regex instead:

$text = preg_replace("/<a[^>]+\>(<img[^>]+\>)<\/a>/i", '$1', $text);

This method is faster and much simpler than plain HTML Dom

+1
source

You can use regex for this:

$string = '<a href=""><img src="" /></a>';
$string = preg_replace('/(<a(?: [^>]+| |)>)(?:[^<]+|)<img(?: [^>]+| |)>(?:[^<]+|)(<\/a>)/','$1$2',$string);

- , <img> , .

<img> , :

$string = '<a href=""><img src="" /></a>';
$string = preg_replace('/(<a(?: [^>]+| |)>)([^<]+|)<img(?: [^>]+| |)>([^<]+|)(<\/a>)/','$1$2$3$4',$string);
0

All Articles