How to replace ereg?

本小妞迷上赌 提交于 2019-11-27 06:51:38

问题


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

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

This is line 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?

Thanks


回答1:


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.

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:

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

So:

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



回答2:


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.




回答3:


the preg_ family expects the regex to be delimited. Instead of:

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

try:

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


来源:https://stackoverflow.com/questions/2217850/how-to-replace-ereg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!