note 46994 added to function.preg-match-all
| From: | php-general at lists dot php dot net | Date: | Fri, 29 Oct 2004 21:25:25 +0000 |
| Subject: | note 46994 added to function.preg-match-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79331@lists.php.net to get a copy of this message | ||
I might be wrong but apparently PHP lacks a way to spot not only matches but their _index_ positions
within a string.
I at first thought that once found the matches by preg_match_all, all one had to do to draw also
their index positions in the input string, was to iterate the returned array of matches and
recursively grab any match from the string by strpos, removing the already inspected substring.
Though it may seem an obvious idea, yet it may not work.
The position in a string searched by a string oriented function is not necessarily the same poistion
searched by a regular expression oriented function.
Consider this example, input string is:
"A thesaurus for the pupil"
whereas the regular expression searches for:
"/the\\b/"
which is obviusly a word like "the" followed by a word boundary (\\b).
The preg_match_all matches would report, correctly, only the isolated article "the", for
that is followed by a word boundary.
But attempting to retrieve the index position of that match by strpos would report the index
position of THEsaurus.
So do _not_ use strpos in combination with preg_match_all having in mind the retrieval of the index
positions of the matches: that won't work the expected way.
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46994
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46994&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46994&report=yes
Search -- http://master.php.net/manage/user-notes.php