how do i find the position of a regex pattern?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

how do i find the position of a regex pattern?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Code: Select all

preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][1];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

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

Post by esibobo »

that was the perfect answer to my problem. thanks man. you are too much. :D
Post Reply