13.2. Switching From ereg to preg
First, you have to add delimiters to your patterns:
When using integers instead of strings as patterns or replacement values, convert the number to hexadecimal and specify it using an escape sequence:
$hex = dechex($number); preg_match("/\x$hex/", 'string');
There are a few major differences between ereg and preg. First, when you use preg functions, the pattern isn't just the string pattern; it also needs delimiters, as in Perl, so it's /pattern/ instead. So:
When choosing your pattern delimiters, don't put your delimiter character inside the regular-expression pattern, or you'll close the pattern early. If you can't find a way to avoid this problem, you need to escape any instances of your delimiters using the backslash. Instead of doing this by hand, call addcslashes( ).
For example, if you use / as your delimiter:
$ereg_pattern = '<b>.+</b>'; $preg_pattern = addcslashes($ereg_pattern, '/');
The value of $preg_pattern is now <b>.+<\/b>.
Adding the i after the closing delimiter makes the change.
Finally, there is one last obscure difference. If you use a number (not a string) as a pattern or replacement value in ereg_replace( ) , it's assumed you are referring to the ASCII value of a character. Therefore, since 9 is the ASCII representation of tab (i.e., \t), this code inserts tabs at the beginning of each line:
$tab = 9; $replaced = ereg_replace('^', $tab, $string);
$converted = ereg_replace(10, 12, $text);
To avoid this feature in ereg functions, use this instead:
$tab = '9';
On the other hand, preg_replace( ) treats the number 9 as the number 9, not as a tab substitute. To convert these character codes for use in preg_replace( ), convert them to hexadecimal and prefix them with \x. For example, 9 becomes \x9 or \x09, and 12 becomes \x0c. Alternatively, you can use \t , \r, and \n for tabs, carriage returns, and linefeeds, respectively.
13.2.4. See Also
Copyright © 2003 O'Reilly & Associates. All rights reserved.