php 如何更换ereg?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2217850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:34:34  来源:igfitidea点击:

How to replace ereg?

phpregexpcreposix-ere

提问by Shawn

I'm getting the following message for some php I have to use but did not write:

对于我必须使用但没有写的某些 php,我收到以下消息:

Deprecated: Function ereg() is deprecated in /opt/lampp/htdocs/webEchange/SiteWeb_V5/inc/html2fpdf.php on line 466

This is line 466:

这是第 466 行:

if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))

I tried simply replacing with preg_match, but it couldn't recognize the = modifier in the regular expression.. I'm not too good with regular expression yet and solving this requires that I learn the regexp ereg needs AND the regexp preg_match needs (which, if I am not mistaken, is different)... Could you guys help me out with this one?

我尝试简单地用 preg_match 替换,但它无法识别正则表达式中的 = 修饰符。 ,如果我没记错的话,是不同的)...你们能帮我解决这个问题吗?

Thanks

谢谢

采纳答案by Gumbo

POSIX extended regular expressions(POSIX ERE, used by ereg) and Perl-combatible regular expressions(PCRE, used by preg_match) are very similar. Except from some special POSIX expressions, PCRE is a superset of POSIX ERE.

POSIX 扩展正则表达式(POSIX ERE,由 使用ereg)和Perl 兼容的正则表达式(PCRE,由 使用preg_match)非常相似。除了一些特殊的 POSIX 表达式,PCRE 是 POSIX ERE 的超集。

That means you just need to put your POSIX ERE regular expressions into delimiters(here /) and escape any occurrence of that character inside the regular expression and you have a valid PCRE regular expression:

这意味着您只需要将 POSIX ERE 正则表达式放入分隔符(此处/)并在正则表达式中转义该字符的任何出现,并且您有一个有效的 PCRE 正则表达式:

/^([^=]*)=["']?([^"']*)["']?$/

So:

所以:

preg_match('/^([^=]*)=["\']?([^"\']*)["\']?$/', $v, $a3)

回答by codaddict

Try:

尝试:

if(preg_match('~^([^=]*)=["\']?([^"\']*)["\']?$~',$v,$a3))

The regex in preg_match needs to be enclosed between a pair of delimiters, which is not the case with the deprecated ereg() function.

preg_match 中的正则表达式需要包含在一对分隔符之间,但已弃用的 ereg() 函数则不是这种情况。

回答by Bart Kiers

the preg_family expects the regex to be delimited. Instead of:

preg_家庭希望的正则表达式来分隔。代替:

'^([^=]*)=["\']?([^"\']*)["\']?$'

try:

尝试:

'/^([^=]*)=["\']?([^"\']*)["\']?$/'