preg_match_all带有单词的url的id

时间:2016-08-09 04:34:40

标签: php preg-match-all

我想通过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);

示例网址格式:

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

我只需要1134133

1 个答案:

答案 0 :(得分:0)

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

类似的东西:

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

preg_match("/\d+/",$finalPath,$matches);

或者如果您非常确定网址模式与OP中的示例完全相似,您甚至可以像

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