我有 HTML 注释被包裹在 Li 和 P 标签中:(

发布于 2024-07-30 02:19:58 字数 490 浏览 6 评论 0原文

我的内容首先是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱了心跳 2024-08-06 02:19:58

编辑:
根据 Zed 和您的评论,我已经做了一些测试,这就是您应该使用的:

$final = preg_replace('/<li><p>[\s]*?<\;!--(.*?)-->\;<\/p><\/li>/m', "<!--$1-->", $z);

这是 RE 的细分:

<li><p>

这很明显,

[\s]*?

因为您在

  • 和注释,但我们想要最少数量的换行符,因此我们使用非贪婪的 *? (它也可以与 * 一起使用)
  • <\;
    

    需要转义 ;

    !--(.*?)--
    

    我们再次使用 *? 所以我们只会匹配这一行(否则,如果你再次有相同的行,它会从第一行到最后一行匹配,

    >\;<\/p><\/li>
    

    与上面相同,

    /m'
    

    因此 php 会将换行符视为空格(我对此不确定,但似乎是在职的)

    EDIT:
    following Zed's and your comments I've done some testing and this is what you should use:

    $final = preg_replace('/<li><p>[\s]*?<\;!--(.*?)-->\;<\/p><\/li>/m', "<!--$1-->", $z);
    

    Here is a breakdown of the RE:

    <li><p>
    

    this is obvious

    [\s]*?
    

    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

    >\;<\/p><\/li>
    

    same as above

    /m'
    

    so php would treat newlines as whitespace (i am not sure about this but it seems to be working)

    唐婉 2024-08-06 02:19:58

    像这样的东西吗?

    $final = preg_replace("/<li><p>(<!--.*-->)<\/p><\/li>/", "$1", $original);
    

    Something like this?

    $final = preg_replace("/<li><p>(<!--.*-->)<\/p><\/li>/", "$1", $original);
    
    素染倾城色 2024-08-06 02:19:58

    @Zed:

    让我们更加关心:

    $final = preg_replace("/<li><p>(<!--.*?-->)<\/p><\/li>/", "$1", $original);
    # use .*? every time over .* unless you specificly want what it does
    # .*? matches as less as it can
    # .* matches as much as it can
    

    甚至更好:

    $final = preg_replace("/<li><p>(<!--[^\-\>]+-->)<\/p><\/li>/", "$1", $original);
    # [^\-\>]+ will look for any character that is not - or > 
    # so will perform faster
    

    只是试图提倡更好的正则表达式实践。 希望这可以帮助。

    @Zed:

    Lets be more caring:

    $final = preg_replace("/<li><p>(<!--.*?-->)<\/p><\/li>/", "$1", $original);
    # use .*? every time over .* unless you specificly want what it does
    # .*? matches as less as it can
    # .* matches as much as it can
    

    even better:

    $final = preg_replace("/<li><p>(<!--[^\-\>]+-->)<\/p><\/li>/", "$1", $original);
    # [^\-\>]+ will look for any character that is not - or > 
    # so will perform faster
    

    Just trying to advocate better regex practice. Hope this helps.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文