我有 HTML 注释被包裹在 Li 和 P 标签中:(
我的内容首先是 htmlentities
,然后是 stripslashes
,最后是 nl2br
。
这意味着最后的水印最终会变成:
<li><p><!-- watermark --></p></li>
不是很有用。 我有下面的代码来尝试删除 html 注释并停止显示,但它不太擅长!
$methodfinal = str_replace('<li><p><!--', '<!--', $method);
$methodfinal2 = str_replace('--></p></li>', '-->', $methodfinal);
echo $methodfinal2;
有人有什么想法吗?
I have content that is first htmlentities
and then stripslashes
followed by nl2br
.
This means a watermark at the end ends up as:
<li><p><!-- watermark --></p></li>
Not very useful. I have the code below to try and strip the html comments and stop it displaying but its not very good at it!
$methodfinal = str_replace('<li><p><!--', '<!--', $method);
$methodfinal2 = str_replace('--></p></li>', '-->', $methodfinal);
echo $methodfinal2;
anyone got any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:
根据 Zed 和您的评论,我已经做了一些测试,这就是您应该使用的:
这是 RE 的细分:
这很明显,
因为您在
需要转义 ;
我们再次使用 *? 所以我们只会匹配这一行(否则,如果你再次有相同的行,它会从第一行到最后一行匹配,
与上面相同,
因此 php 会将换行符视为空格(我对此不确定,但似乎是在职的)
EDIT:
following Zed's and your comments I've done some testing and this is what you should use:
Here is a breakdown of the RE:
this is obvious
because you have a few spaces and a newline between the
<li>
and the comment, but we want the least number of newlines so we use the non greedy *? (it sould work with * as well)need to escape the ;
again we use *? so we would match only this line (other wise if you had the same line again it wold match from the first one to the last one
same as above
so php would treat newlines as whitespace (i am not sure about this but it seems to be working)
像这样的东西吗?
Something like this?
@Zed:
让我们更加关心:
甚至更好:
只是试图提倡更好的正则表达式实践。 希望这可以帮助。
@Zed:
Lets be more caring:
even better:
Just trying to advocate better regex practice. Hope this helps.