mikel atef mikel atef - 3 months ago 23
PHP Question

preg_match_all id of url with words

I want to get the news id from URL with php preg_match_all;

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

$m = preg_match_all($pattern,$uu,$matches);
print_r ($matches);


Example URL formats:

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


i need to get 1134133 only

Answer

Simple way would be get the last part first and then do regex to get numeric part.

Something like:

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

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

or If you're pretty sure that the url patterns are absolutely similar to example in OP you can even simply do like

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