我希望能够从字符串中读取每个图像的src属性,如果匹配,则用不同的格式替换整个img标记。我当前的正则表达式无效。它取代了img标签的开头,但保留了样式,rel,title等所有其他属性。以下是我的逻辑示例:
$message = 'I AM SENDING YOU AND IMAGE <image src="https://www.google.com/logos/doodles/2017/bessie-colemans-125th-birthday-5751652702224384-scta.png" rel="test image" style="display: block;" /> SEE THE IMAGE?';
function replacelinks($message) {
$ret = preg_replace_callback('/(<img\s+src=["\'])([^"\']+)(["\']\s+[^>]+>)/', function ($m) { $url = parse_url($m[1]); if ($url['path'].$url['query'] == "/logos/doodles/2017/bessie-colemans-125th-birthday-5751652702224384-scta.png") { return '{IMG::'.$url['path'].$url['query'].'}'; } }, $message);
return $ret;
}
echo replacelinks($message);
答案 0 :(得分:0)
更改了正则表达式:
/(<img\s+src=["\'])([^"\']+)(["\']\s+[^>]+>)/
到此:
~<img[^>]*?src="(.*?(\w+))".*?>~
有效!