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
2 answers
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