Articles for the ‘Notable PHP Notes’ Category

PostHeaderIcon Solution for the depricated 'ereg' or 'eregi'

Deprecated: Function eregi() is deprecated in….

Why did I get this warning?

As of PHP5.3, ‘ereg()’, ‘eregi()’, ‘ereg_replace()’ are depricated.

How to fix the old code when you upgrade to PHP5.3?

Solution for ereg() or ‘ereg_replace()’or : use ‘preg_match’ and preg_replace’ with the # # or / / RegExp delimiters to wrap around the pattern.

e.g.

old code:

<?php
$string = ‘XYZ’;
if (ereg(‘Z’, $string)) {
echo “‘$string’ contains a ‘Z’!<br />”;
}
?>

Fixed for PHP5.3:

<?php
$string = ‘XYZ’;
if (preg_match(‘/Z/’, $string)) {
echo “‘$string’ contains a ‘Z’!<br />”;
}
?>

old code:

$string = ‘The quick brown fox jumped over the lazy dog.<br />’;
$pattern = ‘quick’;
$replacement = ‘slow’;
echo ereg_replace($pattern,$replacement,$string);

New code for PHP5.3:

$string = ‘The quick brown fox jumped over the lazy dog.<br />’;
$pattern = ‘/quick/’;
$replacement = ‘slow’;
echo preg_replace($pattern, $replacement, $string); // prints “The slow brown fox jumped over the lazy dog.”

Solution for ‘eregi()’ in PHP5.3: use preg_match() and / / or # # dilimters and append i after the last dilimiter

old code:

$file = “paul.Gif”;
echo eregi(“\.(gif)$”,$file)?”$file is a gif\n”:(preg_match(“\.(jpe?g)$”,$file)?”$file is a jeg\n”:”$file is neither gif nor jpeg\n”);

new code:

$file = “paul.Gif”;
echo preg_match(“/\.(gif)$/i“,$file)?”$file is a gif\n”:(preg_match(“/\.(jpe?g)$/”,$file)?”$file is a jeg\n”:”$file is neither gif nor jpeg\n”);

echo “using ## :<br />”

echo preg_match(“#\.(gif)$#i“,$file)?”$file is a gif\n”:(preg_match(“/\.(jpe?g)$/”,$file)?”$file is a jeg\n”:”$file is neither gif nor jpeg\n”);

So, are you ready for PHP5.3?

Bookmark and Share
Follow on Buzz
Scan with your mobile phone!