我必须在下面过滤此网址以获取页码:
http://www.domain.com/string/999/string-article-title/999
我想在此网址格式
中过滤最后3位数字(这与网页相关)我试过这个,但没有成功:
preg_match("/http::\/\/www.domain.com\/string\/999\/string-article-title\/^[0-9]{3}$", $nlink, $matches, PREG_OFFSET_CAPTURE)
如何过滤这些模式,以便我可以从
获取网址"http://www.domain.com/string/999/string-article-title/1 to 999"
PS:抱歉我的英文不好
答案 0 :(得分:2)
你可以999
赞成:
$ret = array_pop(explode('/', $nlink));
答案 1 :(得分:0)
此:
preg_match("/(?<=^http:\/\/www\\.domain\\.com\/string\/999\/string-article-title\/)[1-9][0-9]{0,2}$/", $nlink, $matches);
结果:
Array
(
[0] => 999
)
可以轻松扩展正则表达式以覆盖请求结尾处的任意位数:
/(?<=^http:\/\/www\\.domain\\.com\/string\/999\/string-article-title\/)[1-9][0-9]*$/
答案 2 :(得分:0)
代码:
$nlink='http://www.domain.com/string/999/string-article-title/999';
preg_match("/http:\/\/www.domain.com\/string\/999\/string-article-title\/([0-9]{3})/", $nlink, $matches, PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches);
结果:
Array
(
[0] => Array
(
[0] => http://www.domain.com/string/999/string-article-title/999
[1] => 0
)
[1] => Array
(
[0] => 999
[1] => 54
)
)
答案 3 :(得分:0)
$page_number = substr( $url, strrpos($url, "/") + 1 ); // returns 999
if ( $page_number >= 1 && $page_number <= 999 )
{
// match
}
答案 4 :(得分:0)
没有preg_*
,又简单又简单:
$a = array_reverse(explode("/",rtrim($url,"/")));
echo $a[0];
or
$a = array_pop(explode("/",rtrim($url,"/")));
echo $a;
$a
中假设完整的网址。即使$url = "http://www.test.com/999/";
由于rtrim
而最终带有斜杠,此代码仍然有用。