Php preg_用单词匹配url的所有id
我想用php preg_match_all从URL获取新闻idPhp preg_用单词匹配url的所有id,php,preg-match-all,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); URL格式示例: http://www.xxxx.com/t1134133-0 http://www.xxxx.com/t113413
$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`
简单的方法是先获取最后一部分,然后使用正则表达式获取数字部分 比如:
//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`
数组([0]=>-0[1]=>0)如何处理我的sql选择?您将使用
where news\u ID像“%whater%”
Array([0]=>-0[1]=>0)如何处理我的sql选择?您将使用where news\u ID像“%whater%”