Page 1 of 1

how do i find the position of a regex pattern?

Posted: Mon May 31, 2010 7:36 am
by esibobo
i want to find the position of a regex pattern within a string. i realise i can check for the existence of the pattern with preg_match but what i really need is the position of the found match within the haystack, more like what strpos does.
In short i need a function with the exact behaviour of strpos but that works with regular expressions. can anyone help, please?

Re: how do i find the position of a regex pattern?

Posted: Mon May 31, 2010 10:20 am
by AbraCadaver

Code: Select all

preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];

Re: how do i find the position of a regex pattern?

Posted: Sat Jun 05, 2010 12:03 pm
by esibobo
that was the perfect answer to my problem. thanks man. you are too much. :D