note 77822 deleted from function.preg-replace by danbrown

From: Date: Fri, 28 Nov 2008 19:47:29 +0000
Subject: note 77822 deleted from function.preg-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147141@lists.php.net to get a copy of this message
Note Submitter: Santosh Patnaik ---- Once a match is identified, the regular expression engine appears to set aside the matching segment of the target string. A second segment that you expect to match may therefore end up not getting matched: // Expect 'pa pa pa pa' but get 'pa ma pa ma' echo preg_replace('(^|\s)ma(\s|$)', '$1pa$2', 'ma ma ma ma'); Here the issue can be solved by using a 'lookahead': // Expect and get 'pa pa pa pa' echo preg_replace('(^|\s)ma(?=\s|$)', '$1pa', 'ma ma ma ma');

« previous php.notes (#147141) next »