note 74266 added to function.preg-replace

From: Date: Mon, 02 Apr 2007 21:31:40 +0000
Subject: note 74266 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-125020@lists.php.net to get a copy of this message
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!"); } ---- Server IP: 69.39.81.135 Probable Submitter: 68.163.22.68 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/74266 Del: integrated -- https://master.php.net/note/delete/74266/integrated Del: useless -- https://master.php.net/note/delete/74266/useless Del: bad code -- https://master.php.net/note/delete/74266/bad+code Del: spam -- https://master.php.net/note/delete/74266/spam Del: non-english -- https://master.php.net/note/delete/74266/non-english Del: in docs -- https://master.php.net/note/delete/74266/in+docs Del: other reasons-- https://master.php.net/note/delete/74266 Reject -- https://master.php.net/note/reject/74266 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125020) next »