note 85288 modified in function.preg-replace by felipe
| From: | felipe@php.net | Date: | Thu, 28 Aug 2008 13:41:44 +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-143937@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.
<?php
$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--
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