Why is ereg
deprecated in PHP?
I had a lot of functions which used this, now they always give warning.
What is the alternative of this too?
http://pl.php.net/manual/en/function.ereg.php
Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.
Ereg is deprecated because it was replaced by the the PCRE extension. The reason(s) it was replaced and deprecated is answered in the below link, but to save you some time here is the copy and pasted answer:
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().
One difference between the two is that ereg looks for the longest matching result while preg looks for the first result. Here is the list of differences between the two to help you in determining how best to go about updating your code: http://www.php.net/manual/en/reference.pcre.pattern.posix.php
It should be of note that PHP 6.0 has COMPLETELY removed ereg, so if you are eventually going to be moving your code to a newer server that may use PHP 6.0, the ereg function will no longer be available.
It says why on the documentation page:
Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE.
One possible reason is for performance concerns, as already answered by others.
I'd like to add another (yet) possible reason:
Assume you wrote some code like this (PHP 5.3):
<?php
$arg=$_GET['key'];
if (ereg('^[A-Za-z0-9]+$', $arg) === FALSE){
die('Invalid key');
}
# Do some other things with $arg
?>
One could bypass it by supplying this URL:
foo.php?key=A%00text
^~~~
with the text
part being arbitrarily anything he wants, which leaves a huge security flaw in the code. preg_match()
however, does not have this issue. So you'd better migrate all your code to preg
, as ereg
is no longer available at all in PHP 6.0.
来源:https://stackoverflow.com/questions/3078993/why-is-ereg-deprecated-in-php