简体   繁体   English

PHP preg函数不解析html注释

[英]PHP preg functions dont parse html comments

I have a problem that only occurs on 1 specific linux server. 我有一个问题仅在1个特定的linux服务器上发生。 Its a CentOS 5.7 Apache 2.2 PHP 5.33 server in the back just for the record. 其背面的一台CentOS 5.7 Apache 2.2 PHP 5.33服务器仅供参考。

I have a PHP template class that parses out loops out of HTML comments in the template files with the preg_ functions. 我有一个PHP模板类,可以使用preg_函数解析出模板文件中的HTML注释循环。 Example file: 示例文件:

    <!--if place-->
        {place}
    <!--else place-->
        Nothing
    <!--endif place-->

This is the command i use: 这是我使用的命令:

$found = preg_match("/(?i)<!--(if|begin) ([a-zA-Z0-9_.\->!%<>= ]+)-->/u", $template, $aTag, PREG_OFFSET_CAPTURE, $position);

This should give me a branch with name "place". 这应该给我一个名称为“ place”的分支。

On my local and all other enviroments it parses the if branch correctly, only on this sever it does not parse them and just lets them in the code which means that in my case it shows both the var {place} and the string Nothing. 在我的本地环境和所有其他环境中,它会正确解析if分支,仅在此服务器上它不会解析它们,而只是让它们进入代码,这意味着在我的情况下,它同时显示var {place}和字符串Nothing。

Like i said, it works on local Windows -> WampServer machine. 就像我说的那样,它可以在本地Windows-> WampServer计算机上运行。 It works on other Linux machines, also CENTOS ones. 它可以在其他Linux机器上运行,也可以在CENTOS上运行。

It seems like this machine is not recognizing the HTML comment tags as parsable for the php script. 看来这台机器无法识别HTML注释标签对于php脚本是可解析的。 Is that possible? 那可能吗?

Thanks in advance. 提前致谢。 Any suggestions are greatly appreciated. 任何建议,不胜感激。

You need to add "s" modifier to your expression to force multiline matching mode: 您需要在表达式中添加“ s”修饰符以强制执行多行匹配模式:

"/(?i)<!--(if|begin) ([a-zA-Z0-9_.\->!%<>= ]+)-->/s"

and I'm 100% sure you do not need to use "u" at all. 并且我100%确定您根本不需要使用“ u”。

For anybody that might come accross this also. 对于可能遇到的任何人也是如此。

It was a plesk compiled PHP version (5.3.3) that had a PCRE library from 2006!! 这是一个plesk编译的PHP版本(5.3.3),具有2006年以来的PCRE库! compiled into it, that basically ignored all HTML tags in parsing! 编译进去,基本上在分析时会忽略所有HTML标记!

So when you have this problem, that some stuff gets parsed and some stuff not, check your phpinfo for PCRE extension date. 因此,当您遇到此问题时,有些东西会被解析,而有些东西不会被解析,请检查您的phpinfo以获取PCRE扩展日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM