|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Submitted: 2004-10-29 23:39 UTC Modified: 2004-10-30 16:48 UTC
From: webmaster at unitedscripters dot com Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 5.0.2 OS: Windows XPP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: webmaster at unitedscripters dot com
New email:
PHP Version: OS:


 [2004-10-29 23:39 UTC] webmaster at unitedscripters dot com

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:
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.

Reproduce code:
function foo($string, $regexp){
preg_match_all($regexp, $string, $matches);
for($m=0; $m < $matchSize; $m++){
$found=strlen(substr($string, 0, $found));
preg_match($regexp, $string, $specificMatch, PREG_OFFSET_CAPTURE, $found);
strpos(substr($string, $found), $specificMatch[0][0]);/*shortcoming: it's not a real index*/
return $indexes;

$in="A thesaurus for the pupil";
print "In string <strong>$in</strong>, match is: ";
$out=foo($in, "/the\\b/");
print "<br>Wrong Index reported: ";

Expected result:
The result is correct, it is the feature that we lack and that _apparently_ we cannot even implement: grabbing the correct index of a Regular Expression match.
Whatever the case, the feature is needed: javascript has it, the regular expression oriented function named search(), which reports at least one index and thus can be used recursively on gradually shrinking substrings of the input string to retrieve the positions of all the matches.

If there is a way and I was not aware of it, I apologize. Yet the list of perl regexps clearly lacks a function for the retrieval of the indexes.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-30 14:27 UTC] webmaster at unitedscripters dot com
Setting the PREG_OFFSET_CAPTURE flag in preg_match_all, does that.

I apologize for the wrong submission of an alleged missing feature.

Luckly enough, this is the only wrong submission I sent. I'll be more careful in the future when I report something at around 5am italian time after 15 hours of coding!
 [2004-10-30 16:48 UTC]
User error, so we mark the bug as bogus.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Dec 09 09:01:27 2023 UTC