我不懂正则表达式。我需要更改以这种方式格式化的 img 标签:<img class="special-class" (then custom srcs and whatever) />
. 我认为 preg_replace 会完成这项工作还是会有更有效的方法?
我只需要正则表达式的方式来搜索带有第一类的完整 img 标记,这样我就可以用另一个保存为 $buzz 的字符串替换它
我不懂正则表达式。我需要更改以这种方式格式化的 img 标签:<img class="special-class" (then custom srcs and whatever) />
. 我认为 preg_replace 会完成这项工作还是会有更有效的方法?
我只需要正则表达式的方式来搜索带有第一类的完整 img 标记,这样我就可以用另一个保存为 $buzz 的字符串替换它
要匹配一个完整的 img 标签(使用有效的 html 并且其中没有未引用>
):
preg_match('#<img([^>]+)>#i', $html, $match);
不知道你还想要什么...
$match
将包含完整的 in[0]
和 in 的属性(作为字符串)[1]
。
如果要匹配类属性:
preg_match('#class="([^"])"#i', $html, $match);
编辑
嗯,这可能有效:
preg_replace('#<img[^>]+class="[^"]*replace[^"]*"[^>]*>#', '', $html);
如果我输入:
<img src="oele" class="boele" />
<img src="boele" />
<img src class="replace boele">
it only replaces the last img and leaves the rest untouched.
Regex rules!
我会选择 Javascript 解决方案(想想 jQuery)。如果您愿意,我可以提供帮助