note 22014 added to function.preg-replace
| From: | lonestarn8 at netscape dot com | Date: | Mon, 03 Jun 2002 22:39:06 +0000 |
| Subject: | note 22014 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31399@lists.php.net to get a copy of this message | ||
Just a note regarding the "limit" flag...
I was trying to parse an XML file looking for an entire Element, with an Attribute that matched a
certain ID.
Well, when the Element to be matched was in the middle of my XML file, the preg_replace would come
back false. But when the Element to be matched was at the beginning of the file, the preg would
match greedily, and slurp in the whole file.
So what I had to do was specify a LIMIT in my preg_replace. Since I knew that there should only be
one element with any given Attribute, I set the limit to "1", and it works great.
So if you're having problems with matches in the middle of your search string, try setting a
"limit" on the number of matches.
Syntax eg:
$slurper = preg_replace($pattern, $replace, $subject, 1);
Hope this helps.
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+22014
http://master.php.net/manage/user-notes.php?action=delete+22014
http://master.php.net/manage/user-notes.php?action=reject+22014