Since POSIX regular expressions (ereg) are deprecated since PHP 5.3.0, I'd like to know an easy way to convert the old expressions to PCRE (Perl Compatible Regular Expressions) (preg).
Per example, I have this regular expression:
eregi('^hello world');
How can I translate expressions into preg_match
compatible expressions?
Note: This post serves as a placeholder for all posts related to conversion from ereg to preg, and as a duplicate options for related questions. Please do not close this question.
Related:
There are more differences between
ereg()
andpreg_replace()
than just the syntax:Return value:
FALSE
ereg()
returnsFALSE
,preg_match()
returns0
ereg()
returns string length or1
,preg_match()
returns always1
Resulting array of matched substrings: If some substring is not found at all (
(b)
in...a(b)?
), corresponding item inereg()
result will beFALSE
, while inpreg_match()
it will not be set at all.If one is not brave enough to convert his or her
ereg()
topreg_match()
, he or she may use mb_ereg(), which is still available in PHP 7.The biggest change in the syntax is the addition of delimiters.
Delimiters can be pretty much anything that is not alpha-numeric, a backslash or a whitespace character. The most used are generally
~
,/
and#
.You can also use matching brackets:
If your delimiter is found in the regular expression, you have to escape it:
You can easily escape all delimiters and reserved characters in a string by using preg_quote:
Also, PCRE supports modifiers for various things. One of the most used is the case-insensitive modifier
i
, the alternative to eregi:You can find the complete reference to PCRE syntax in PHP in the manual, as well as a list of differences between POSIX regex and PCRE to help converting the expression.
However, in your simple example you would not use a regular expression:
From PHP version 5.3,
ereg
is deprecated.Moving from
ereg
topreg_match
is just a small change in our pattern.First, you have to add delimiters to your code, e.g.:
to
For
eregi
case-insensitive matching, puti
after the last delimiter, e.g.:to
Ereg replacement with preg(as of PHP 5.3.0) was right move in our favor.
preg_match, which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg.
You should know 4 main things to port ereg patterns to preg:
Add delimiters(/):
'pattern' => '/pattern/'
Escape delimiter if it is a part of the pattern:
'patt/ern' => '/patt\/ern/'
Achieve it programmatically in following way:
$old_pattern = '<div>.+</div>';
$new_pattern = '/' . addcslashes($old_pattern, '/') . '/';
eregi(case-insensitive matching):
'pattern' => '/pattern/i'
So, if you are using eregi function for case insenstive matching, just add 'i' in the end of new pattern('/pattern/').ASCII values: In ereg, if you use number in the pattern, it is assumed that you are referring to the ASCII of a character. But in preg, number is not treated as ASCII value. So, if your pattern contain ASCII value in the ereg expression(for example: new line, tabs etc) then convert it to hexadecimal and prefix it with \x.
Example: 9(tab) becomes \x9 or alternatively use \t.