note 76744 added to function.preg-replace
| From: | flar at forgetably dot com | Date: | Sun, 29 Jul 2007 00:07:33 +0000 |
| Subject: | note 76744 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129012@lists.php.net to get a copy of this message | ||
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).
----
Server IP: 69.147.83.197
Probable Submitter: 89.176.63.86
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/76744
Del: integrated -- https://master.php.net/note/delete/76744/integrated
Del: useless -- https://master.php.net/note/delete/76744/useless
Del: bad code -- https://master.php.net/note/delete/76744/bad+code
Del: spam -- https://master.php.net/note/delete/76744/spam
Del: non-english -- https://master.php.net/note/delete/76744/non-english
Del: in docs -- https://master.php.net/note/delete/76744/in+docs
Del: other reasons-- https://master.php.net/note/delete/76744
Reject -- https://master.php.net/note/reject/76744
Search -- https://master.php.net/manage/user-notes.php