Life expectancy ereg, switching to preg

I am working on a large PHP application (> 1 million lines, 10 years) that makes extensive use of ereg and ereg_replace - there are currently 1,768 unique regular expressions in 516 classes.

I really understand why ereg deprecated, but it is clearly ported to preg .

Does anyone know how long ereg support can be supported in PHP and / or have any recommendations for porting to preg at this scale. I suspect automatic translation from ereg to preg is impossible / impractical?

+8
php regex ereg
source share
4 answers

I'm not sure when ereg will be deleted, but my bid is PHP 6.0.

Regarding your second problem (translating ereg to preg ) doesn’t seem something difficult, if your application has> 1 million lines, you probably should have resources to force someone to do this work for a maximum of a week, I would grep all instances of ereg_ in your code and configure some macros in your favorite IDE (simple things like adding separators, modifiers, etc.).

I bet most of the 1768 regular expressions can be ported using a macro, and the rest, well, are good pairs of eyes.

Another option would be to write wrappers around the ereg functions, if they are not available, change if necessary:

 if (function_exists('ereg') !== true) { function ereg($pattern, $string, &$regs) { return preg_match('~' . addcslashes($pattern, '~') . '~', $string, $regs); } } if (function_exists('eregi') !== true) { function eregi($pattern, $string, &$regs) { return preg_match('~' . addcslashes($pattern, '~') . '~i', $string, $regs); } } 

You get the idea. In addition, the PEAR package PHP compatibility can also be a viable solution.


Differences from POSIX Regular Expression

Starting with PHP 5.3.0, the POSIX Regex extension is deprecated. There are a number of differences between POSIX regular expression and PCRE regular expression. This page lists the most famous ones you need to know when upgrading to PCRE.

  • PCRE functions require the template to be enclosed in delimiters.
  • Unlike POSIX, the PCRE extension does not have special functions for case insensitivity. Instead, it is supported using the template / i modifier. Other template modifiers are also available to change the matching strategy.
  • POSIX functions find the longest leftmost match, but PCRE stops in the first valid match. If the string does not match at all it does not matter, but if it matches it can have dramatic consequences for both the resulting match and match the speed. To illustrate this difference, consider the following example from Jeffrey Fridle's Mastering Regular Expressions. Using the template alone (myself)? (self-sufficient)? if the string is correct using PCRE will lead to a match, but using POSIX, the result will be a complete string. Both (sub) match the original string, but POSIX requires the result to be the longest.
+2
source share

My intuition says that they never intend to remove ereg . PHP still supports really old and obsolete things like register globals. There are just too many outdated applications. However, there is a small chance that the extension should be removed because someone is discovering a serious vulnerability, and there only no one can fix it.

In any case, it is worth noting that:

  • You are not required to update your PHP installation. This is quite common when legacy servers run legady applications.

  • PHP_Compat PEAR package offers a simple PHP version of some native functions. If ereg disappears, you can add it.


By the way ... Actually, PHP 6 is dead . They realized that their approach to making PHP fully Unicode compatible was more complicated than they thought, and they were rethinking it all. Conclusion: you can never make perfect predictions.

+2
source share

I had this problem on a much smaller scale - the application is more like 10,000 lines. In each case, all I have to do is switch to preg_replace() and put the delimiters around the regex pattern.

Anyone should do this - not even a programmer can be given a list of file names and line numbers.

Then just run your tests to keep track of any crashes that can be fixed.

The ereg functions will be removed from PHP6, by the way - http://jero.net/articles/php6 .

+1
source share

All ereg functions will be removed with PHP 6, I suppose.

0
source share

All Articles