preg_match_all id of url with words


preg_match_all id of url with words

我想用php preg_match_all从URL获取新闻id;

     $pattern = '%^(http://xxxxx/t)([0-9].?)+^(-)+(:[0-9]+)?(/.*)?$%i';
         $m = preg_match_all($pattern,$uu,$matches);    
print_r ($matches);

URL格式示例:

http://www.xxxx.com/t1134133-0
http://www.xxxx.com/t1134133-news-news-worlds
http://www.xxxx.com/t1134133-mi%20&ffjfj

我只需要得到1134133

简单的方法是先得到最后一部分,然后用正则表达式得到数字部分。

类似:

//get the last part
$arrayUrl=explode("/",$url);
$finalPath=$arrayUrl[count($arrayUrl)-1];
preg_match("/'d+/",$finalPath,$matches);

或者如果你非常确定url模式与OP中的示例绝对相似,你甚至可以简单地使用

preg_match("/'d+/",$url,$matches); // where url is `http://www.xxxx.com/t1134133-mi%20&ffjfj`