Converting POSIX RegExpExpressions to PCRE (preg) in PHP
Since POSIX Regular Expressions (ereg) have been deprecated in PHP since version 5.3.0, migrating to Perl Compatible Regular Expressions (PCRE) is essential. Here's a guide to convert your old expressions to preg-compatible counterparts:
Delimiters:
The most significant change is the introduction of delimiters, which enclose the regular expression. They can be ~, /, #, or brackets: [], (), or {}.
Escape Characters:
If the chosen delimiter is present within the expression, escape it with a backslash (). Use preg_quote to escape all delimiters and reserved characters.
Case Sensitivity Modifier:
PCRE introduces the "i" modifier for case-insensitive matching, similar to eregi.
Simple Matches:
In cases like your example (ereg('^hello world')), a simple strpos would suffice:
stripos($str, 'hello world') === 0
Converting Example:
eregi('^hello world'); // POSIX expression // PCRE conversion with delimiters and case-insensitive modifier preg_match('/^hello world/i', $str);
Further Resources:
The above is the detailed content of How Do I Convert POSIX Regular Expressions to PCRE in PHP?. For more information, please follow other related articles on the PHP Chinese website!