note 85288 modified in function.preg-replace by felipe

From: Date: Sun, 24 Aug 2008 04:27:56 +0000
Subject: note 85288 modified in function.preg-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143783@lists.php.net to get a copy of this message
preg_replace is greedy by default, and the behaviour of ? on the pattern is to make it non-greedy contrary to what i read on PCRE docs. $str ="asdfd adsfd aaaadasd"; $str = preg_replace("/(a)(.*)(d)/","a($2)d",$str); a(sdfd adsfd aaaadas)d $str = preg_replace("/(a)(.*)?(d)/U","a($2)d",$str); a(s)dfd a()dsfd a(aaa)da(s)d which is what i wanted. --was-- correction. preg_replace is greedy by default, and the behaviour of ? on the pattern is to make it non-greedy contrary to what i read on PCRE docs. $str ="asdfd adsfd aaaadasd"; $str = preg_replace("/(a)(.*)(d)/","a($2)d",$str); a(sdfd adsfd aaaadas)d $str = preg_replace("/(a)(.*)?(d)/U","a($2)d",$str); a(s)dfd a()dsfd a(aaa)da(s)d which is what i wanted. http://php.net/manual/en/function.preg-replace.php

« previous php.notes (#143783) next »