php - how to remove whole img element with a SRC value in an UTF-8 string? -
for example algorithm:
$text = 'some text1 <img src="1.jpg" /> text2 <img src="2.jpg">'; if($src == '1.jpg') remove(img tag in $text) echo htmlspecialchars($text, ent_quotes, "utf-8") and result must be:
some text1 text2 <img src="2.jpg>"
use preg_replace directly, no condition , preg_match needed.
$text = 'some text1 <img src="1.jpg" /> text2 <img src="2.jpg">'; echo preg_replace('~(<img.*[\'"]1\.jpg[\'"].*>)~', '', $text); // text1 text2 <img src="2.jpg"> the first ['"] needed avoid removing eg. a1.jpg file, second 1 optional. it's better understand how works.
update
due comment below, here updated version variable name:
$file = '1.jpg'; $text = 'some text1 <img src="1.jpg" /> text2 <img src="2.jpg" />'; echo preg_replace('~<img[^>]+[\'"]' . $file . '[\'"].*?/>~', '', $text);
Comments
Post a Comment