phpregexquery-stringtext-parsingurl-parsing

Get integer value from malformed query string


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


Solution

  • $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