PHP Regex:删除HTML属性的引用,包括img src

时间:2021-12-28 22:29:12

I have this regex:

我有这个正则表达式:

//remove quotes from HTML attributes that does not contain spaces; keep quotes around URLs

var $result = preg_replace('/((\S)+\s*(?<!href)(?<!src)(=)\s*)(\"|\')(\S+)(\"|\')/','$1$5', $string);

It's almost working as intended, I just need to adapt it with an additional exception: allow remove quotes from "src" attributes of "img" tags.

它几乎按预期工作,我只需要修改它另外一个例外:允许从“img”标签的“src”属性中删除引号。

Could please someone give me a tip? Any help would be greatly appreciated.

可以请有人给我一个提示吗?任何帮助将不胜感激。

1 个解决方案

#1


0  

Found a bad coded solution by adaptation, sorry I'm not a regex expert.

通过改编找到了错误的编码解决方案,抱歉,我不是正则表达式专家。

$imgTag = '<img src="image.jpg"/>';
$imgTag = preg_replace('/((\S)+\s*(?<=src)(=)\s*)(\"|\')(\S+)(\"|\')/','$1$5',$imgTag);

Results:

<img src=image.jpg/>

#1


0  

Found a bad coded solution by adaptation, sorry I'm not a regex expert.

通过改编找到了错误的编码解决方案,抱歉,我不是正则表达式专家。

$imgTag = '<img src="image.jpg"/>';
$imgTag = preg_replace('/((\S)+\s*(?<=src)(=)\s*)(\"|\')(\S+)(\"|\')/','$1$5',$imgTag);

Results:

<img src=image.jpg/>