note 46994 modified in function.preg-match-all by tularis

From: Date: Fri, 29 Oct 2004 21:54:48 +0000
Subject: note 46994 modified in function.preg-match-all by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79334@lists.php.net to get a copy of this message
---[ 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. --was-- 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. http://php.net/manual/en/function.preg-match-all.php

« previous php.notes (#79334) next »