How to replace ereg?
Asked Answered
B

4

11

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

Benham answered 7/2, 2010 at 18:51 Comment(1)
Possible duplicate of How can I convert ereg expressions to preg in PHP?Invagination
B
8

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)
Bewitch answered 7/2, 2010 at 19:6 Comment(1)
Not really a superset (besides character classes), e.g. matching /AB|ABC/ on ABCD with ERE will give the longest match (ABC) but PCRE will give the first match (AB).Gracie
F
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.

Fia answered 7/2, 2010 at 18:53 Comment(0)
D
1

Copied by other contributor:

This not is a replace is recover ereg function:

if(!function_exists('ereg'))            { function ereg($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/', $subject, $matches); } }
if(!function_exists('eregi'))           { function eregi($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/i', $subject, $matches); } }
if(!function_exists('ereg_replace'))    { function ereg_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/', $replacement, $string); } }
if(!function_exists('eregi_replace'))   { function eregi_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/i', $replacement, $string); } }
if(!function_exists('split'))           { function split($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/', $subject, $limit); } }
if(!function_exists('spliti'))          { function spliti($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/i', $subject, $limit); } }
Dani answered 25/8, 2023 at 13:1 Comment(1)
What contributor did this come from? And are you saying this doesn’t work? Reading the code, this appears to offer a polyfill for the deprecated functions. Have you tested with the OP’s specific expression?Onder
S
0

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

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

try:

'/^([^=]*)=["\']?([^"\']*)["\']?$/'
Stockbroker answered 7/2, 2010 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.