Search code examples
phpregexdeprecatedpcreposix-ere

Eregi to preg_replace change for php 5.3 compatibility


I have this line in one of my scripts and its throwing a deprecated error.

 eregi_replace( '\.([a-z]{3,4})$', "-{$width}x{$height}.\\1", $src );

Can someone show me how to turn this into preg_replace and tell me why and which bits of it need to change so I can learn for future changes? I have had a go myself but where this bit of code is means its really hard to test!!

Is it as simple as purely replacing the eregi_replace with preg_replace?

I hate regular expressions :)


Solution

  • You need delimiters like / and the i modifier:

    /\.([a-z]{3,4})$/i
    

    So:

    preg_replace('/\.([a-z]{3,4})$/i', "-{$width}x{$height}.\\1", $src);
    

    See this manual page for the differences between POSIX ERE and PCRE.