note 74266 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 20:09:43 +0000 |
| Subject: | note 74266 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127720@lists.php.net to get a copy of this message | ||
Note Submitter: nomex45 at nospam dot yahoo dot com
----
Unlike preg_match, preg_replace will not return FALSE if an invalid regex pattern is fed to it.
However, preg_replace will simply not set the variable it's being told to set. We can then
detect that preg_replace didn't execute properly and by that deduce that the regex pattern is
malformed.
This might make it more clear.
<?PHP
$badregex = '/([a-z]/';
// Errors are suppressed here for security reasons. So how are we supposed to know if the regex
pattern is wrong?
$return = @preg_replace($badregex, "This is a test.", "*");
// If preg_replace detects that the regex pattern is wrong, it will unset() the variable it's
being stored to.
if (!isset($return)) {
die("Bad regex pattern fed to preg_replace!");
}