php 不推荐使用的函数删除和替换问题
我正在尝试用新推荐的函数替换 PHP 源代码中已弃用的函数。但是,按照 php 手册中的建议,用正则表达式模式中的“i”将 eregi 函数替换为 preg_match 函数时遇到了一个大问题。下面是示例代码:
<?php
$strXml=<<<XMLSTRING
ALIPC231232
TIME
Jan 21 10:43:58 UTC 2011
ORIGINAL REQUEST:
TIME PLEASE
XMLSTRING;
eregi("(TIME)(.*)(ORIGINAL REQUEST:)" , $strXml, $matches);
echo "begin_ck_eregi_match1:".$matches[1].":end_ck_eregi_match1";
echo "begin_ck_eregi_match2:".$matches[2].":end_ck_eregi_match2";
preg_match("/(TIME)(.*)(ORIGINAL REQUEST:)/i" , $strXml, $match);
echo "begin_ck_preg_match_match1:".$match[1].":end_ck_preg_match_match1";
echo "begin_ck_preg_match_match2:".$match[2].":end_ck_preg_match_match2";
?>
在上面的代码中,当根据模式测试字符串时,eregi 正确地给出了匹配;但是当 preg_match 用于相同的字符串并针对相同的模式对其进行测试时,不会返回任何匹配项。我不明白为什么?我一定是在这里错过了一些东西。请您帮我解决这个问题。
I am trying to replace the deprecated functions in my PHP source code with new recommended ones. But I was having a big problem with replacing eregi function with preg_match function with an "i" in the regex pattern as suggested in the php manual. Here is the sample code:
<?php
$strXml=<<<XMLSTRING
ALIPC231232
TIME
Jan 21 10:43:58 UTC 2011
ORIGINAL REQUEST:
TIME PLEASE
XMLSTRING;
eregi("(TIME)(.*)(ORIGINAL REQUEST:)" , $strXml, $matches);
echo "begin_ck_eregi_match1:".$matches[1].":end_ck_eregi_match1";
echo "begin_ck_eregi_match2:".$matches[2].":end_ck_eregi_match2";
preg_match("/(TIME)(.*)(ORIGINAL REQUEST:)/i" , $strXml, $match);
echo "begin_ck_preg_match_match1:".$match[1].":end_ck_preg_match_match1";
echo "begin_ck_preg_match_match2:".$match[2].":end_ck_preg_match_match2";
?>
In the above code, eregi gave matches properly when a string is tested against a pattern; but when preg_match is used over the same string and tested it against the same pattern no matches are returned. I am unable to figure out why? I must me missing something here. Request you to please help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
点
.
不会匹配换行符。此外,使用s
修饰符(称为PCRE_DOTALL
):The dot
.
won't match new-lines. Additionally, use thes
modifier (calledPCRE_DOTALL
):eregi() 函数
使情况-* 在*敏感搜索中。您必须使用 PCREi
修饰符以达到相同的结果。eregi() function
makes case-*in*sensitive search. You'll have to use PCREi
modifier to achieve the same results.