note 46994 deleted from function.preg-match-all by nlopess
| From: | nlopess@php.net | Date: | Wed, 07 Dec 2005 22:08:41 +0000 |
| Subject: | note 46994 deleted from function.preg-match-all by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99709@lists.php.net to get a copy of this message | ||
Note Submitter:
----
---[ Editor's Note ]---
You can use the PREG_OFFSET_CAPTURE to do just this. Capture the offset, thus the position of the
start of the search string.
---[ End Note ]---
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.