preg_match_all url 的 id 和单词

preg_match_all id of url with words

我想从 URL 获取新闻 ID php preg_match_all;

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