php preg_replace 特定 html 注释标签之间的所有内容 [英] php preg_replace everything in between specific html comment tags
本文介绍了php preg_replace 特定 html 注释标签之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经检查了其他答案,但似乎无法执行以下操作.请帮助某人:)
I've checked other answers but can't seem to do the following. Please help someone :)
我想删除中间的所有内容,包括特定的 html 注释
I want to remove everything in between and including specific html comments
HTML:
Some HTML that must stay
<!-- START IF USER_ID -->
some html that must go
<!-- END IF USER_ID -->
Some more HTML that's gotta stay
<!-- START IF USER_ID -->
this also needs to go
<!-- END IF USER_ID -->
所以 和
之间的所有内容以及评论本身需要去
So everything in between <!-- START IF USER_ID -->
and <!-- END IF USER_ID -->
and the comments itself needs to go
我的 preg_replace 模式(这显然是错误的):
"/<!-- START IF USER_ID -->.*?<!-- END IF USER_ID -->/"
结果应该是
Some HTML that must stay
Some more HTML that's gotta stay
感谢您提前检查和回答:)
Thanks for checking and for the answers in advance :)
推荐答案
感谢 @mlwacosmos - 使用您提供的链接.
Thanks @mlwacosmos - Using the link you provided.
实现:
$startPoint = '<!-- START IF USER_ID -->';
$endPoint = '<!-- END IF USER_ID -->';
$result = preg_replace('#('.preg_quote($startPoint).')(.*)('.preg_quote($endPoint).')#siU', '', $html);
这篇关于php preg_replace 特定 html 注释标签之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文