从字符串中获取部分

Zaf*_*fee 1 php regex string explode

我正在尝试从网站收集所有CatID ... URL结构是......

http://www.abc.com/family/index.jsp?categoryId=12436777

我感兴趣的categoryId是这种情况12436777

我的问题是

哪个最好,正则表达式或字符串爆炸?

如果正则表达式,请帮助我,我非常糟糕..

另外,我必须考虑像这样的URL

http://www.abc.com/product/index.jsp?productId=12282785

我试过了

$array = explode("http://www.abc.com/family/index.jsp?categoryId=", $loc); 

foreach ($array as $part) {
        $zee[] = $part[1];
}
Run Code Online (Sandbox Code Playgroud)

但它没有给我任何东西..

感谢帮助..

Tim*_*ain 6

您可以使用parse_url可靠地为您提供查询字符串:

$parts = parse_url('http://www.abc.com/family/index.jsp?categoryId=12436777');
Run Code Online (Sandbox Code Playgroud)

然后parse_str解析出变量:

parse_str($parts['query'], $result);
echo $result['categoryId']; // outputs 12436777
Run Code Online (Sandbox Code Playgroud)