PHP Using RegEx to get substring of a string

MonkeyBlue picture MonkeyBlue · May 9, 2011 · Viewed 105.9k times · Source

I'm looking for an way to parse a substring using PHP, and have come across preg_match however I can't seem to work out the rule that I need.

I am parsing a web page and need to grab a numeric value from the string, the string is like this

producturl.php?id=736375493?=tm

I need to be able to obtain this part of the string:

736375493

Thanks Aaron

Answer

David Fells picture David Fells · May 9, 2011
$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);

This is safe for if the format changes. slandau's answer won't work if you ever have any other numbers in the URL.

php.net/preg-match