php 不推荐使用的函数删除和替换问题

发布于 2024-10-13 22:28:58 字数 824 浏览 9 评论 0原文

我正在尝试用新推荐的函数替换 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 技术交流群。

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

发布评论

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

评论(2

我恋#小黄人 2024-10-20 22:28:58

. 不会匹配换行符。此外,使用 s 修饰符(称为 PCRE_DOTALL):

preg_match('/(TIME)(.*)(ORIGINAL_REQUEST:)/is', $strXml, $match);

The dot . won't match new-lines. Additionally, use the s modifier (called PCRE_DOTALL):

preg_match('/(TIME)(.*)(ORIGINAL_REQUEST:)/is', $strXml, $match);
澉约 2024-10-20 22:28:58

eregi() 函数 使情况-* *敏感搜索中。您必须使用 PCRE i修饰符以达到相同的结果。

eregi("(TIME)(.*)(ORIGINAL REQUEST:)" , $strXml, $matches)

preg_match('/(TIME)(.*)(ORIGINAL REQUEST\:)/si', $strXml, $matches);

eregi() function makes case-*in*sensitive search. You'll have to use PCRE i modifier to achieve the same results.

eregi("(TIME)(.*)(ORIGINAL REQUEST:)" , $strXml, $matches)

preg_match('/(TIME)(.*)(ORIGINAL REQUEST\:)/si', $strXml, $matches);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文