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

Gumbo
  • 643,351
  • 109
  • 780
  • 844
Shawn
  • 10,931
  • 18
  • 81
  • 126
  • Possible duplicate of [How can I convert ereg expressions to preg in PHP?](https://stackoverflow.com/questions/6270004/how-can-i-convert-ereg-expressions-to-preg-in-php) – Meloman Sep 14 '17 at 12:44

4 Answers4

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)
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 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`). – kennytm Feb 07 '10 at 19:12
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.

codaddict
  • 445,704
  • 82
  • 492
  • 529
0

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

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

try:

'/^([^=]*)=["\']?([^"\']*)["\']?$/'
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
0

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); } }
  • 2
    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? – Jeremy Caney Aug 25 '23 at 13:20