note 76744 deleted from function.preg-replace by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 02:17:50 +0000 |
| Subject: | note 76744 deleted from function.preg-replace by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133101@lists.php.net to get a copy of this message | ||
Note Submitter: flar at forgetably dot com
----
Special care needs to be taken, since all regular expressions are "greedy" by default.
What does it mean? Example:
Text: 'the big scary house here and the big weird house over there'
Expression '/big(.*)house/'
Matches: 'big scary house here and the big weird house'
In other words, the expression matches everything between the first occurrence of the word
"big" and the last occurrence of the word "house".
If you need to preserve the words 'here and the', you need a non-greedy expression.
If you need a non-greedy regular expression in PHP, add the character "U" after the final
slash. So our updated example will look like this:
Expression: '/big(.*)house/U'
Match 1: 'big scary house'
Match 2: 'big weird house'
Note that with the non-greedy expression we now got two matches and the words between the matches
are preserved (not replaced).