Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我将HTML注释包装在Li和P标记中:(_Php_Html_Regex_Preg Replace - Fatal编程技术网

Php 我将HTML注释包装在Li和P标记中:(

Php 我将HTML注释包装在Li和P标记中:(,php,html,regex,preg-replace,Php,Html,Regex,Preg Replace,我的内容首先是htmlentities,然后是stripslashes,然后是nl2br 这意味着结尾处的水印最终为: <li><p><!-- watermark --></p></li> 不是很有用。我有下面的代码来尝试剥离html注释并停止它的显示,但它不是很好 $methodfinal = str_replace('<li><p><!--', '<!--', $method); $meth

我的内容首先是
htmlentities
,然后是
stripslashes
,然后是
nl2br

这意味着结尾处的水印最终为:

<li><p><!-- watermark --></p></li>
  • 不是很有用。我有下面的代码来尝试剥离html注释并停止它的显示,但它不是很好

    $methodfinal = str_replace('<li><p><!--', '<!--', $method);
    $methodfinal2 = str_replace('--></p></li>', '-->', $methodfinal);
    echo $methodfinal2;
    
    $methodfinal=str_replace(“
  • ”、“-->”、$methodfinal); echo$methodfinal2;
  • 有人有什么想法吗?

    像这样的想法吗

    $final = preg_replace("/<li><p>(<!--.*-->)<\/p><\/li>/", "$1", $original);
    
    $final=preg_replace(“/
  • ()/”、“$1”、$original);
  • 编辑: 以下是Zed和您的评论,我已经做了一些测试,这是您应该使用的:

    $final = preg_replace('/<li><p>[\s]*?&lt\;!--(.*?)--&gt\;<\/p><\/li>/m', "<!--$1-->", $z);
    
    因为在
  • 和注释之间有一些空格和换行符,但我们希望换行符的数量最少,所以我们使用非贪婪*?(它也可以与*一起使用)

    需要逃避现实

    !--(.*?)--
    
    同样,我们使用*?所以我们只匹配这一行(另一方面,如果您再次使用同一行,它将从第一行匹配到最后一行)

    &gt\;<\/p><\/li>
    
    因此php会将换行符视为空白(我不确定这一点,但它似乎起作用了)

    @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(“/
  • ()/”、“$1”、$original); #每次都使用。*?除非您特别想要它的功能 #*?尽可能少地匹配 #尽可能多地匹配。*
  • 更好的是:

    $final = preg_replace("/<li><p>(<!--[^\-\>]+-->)<\/p><\/li>/", "$1", $original);
    # [^\-\>]+ will look for any character that is not - or > 
    # so will perform faster
    
    $final=preg_replace(“/
  • ()/”、“$1”、$original); #[^\-\>]+将查找任何不是-或> #因此,他们的表现会更快

  • 只是想提倡更好的正则表达式实践。希望这能有所帮助。

    你能说明你期望的输出是什么吗?他想从输出中消除空[li][p],我猜$methodfinal2会回应
  • 内容
  • 内容是的,现在我们只希望没有>在评论中签名;)丹尼尔,它不起作用了,因为你的html源代码不正确。评论有!——而且——在侧面而不是@Daniel,Zed是对的,请不要使用我写的东西。@Zed你是对的,我应该使用非贪婪?*但是我现在没有任何方法来测试这个。抱歉。所以$final=preg_replace(/
  • (<!-[^>]*)/“,$1”,$original);不起作用?有人猜一猜吗?替换两个标签应该不会太难吧?最终的获奖条件是:preg_replace('/
  • [\s]*?<\!(.*?->\/m','',“,$original”);能以同样的方式进行改进吗?使用[\s]没有意义*?。您应该使用\s*。如果您有一组字符,例如空格或数字,您可以使用[\s\d]或[\s0-9]。要使其匹配多个字符,请在其后使用+(1或更多)或*(零或更多)或?(无或一)。然后考虑使用?匹配零或更多字符(尽可能最小匹配)。您可以理解\s?不如。*?那么请在此处使用\s+(至少一个空格,可能更多)或\s*(零个或多个空格)或\s?(零个或一个空格)。preg_replace('/
  • \s*<\!-(.?-->\/m','',$original);
    &gt\;<\/p><\/li>
    
    /m'
    
    $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