过滤URL获得页码与正则表达式在PHP上


Filtering URL To Get Page Number With Regex On PHP

我必须过滤下面的URL来获得页码:

http://www.domain.com/string/999/string-article-title/999

我想在这个url模式中过滤最后3位数字(这与页面相关)

我已经试过了,但是没有成功:

preg_match("/http::'/'/www.domain.com'/string'/999'/string-article-title'/^[0-9]{3}$", $nlink, $matches, PREG_OFFSET_CAPTURE)

我如何过滤这些模式,以便我可以从

获得URL
"http://www.domain.com/string/999/string-article-title/1 to 999"

PS:对不起,我的英语不好

你可以得到999:

$ret = array_pop(explode('/', $nlink));

代码:

 $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
        )
)
$page_number = substr( $url, strrpos($url, "/") + 1 ); // returns 999
if ( $page_number >= 1 && $page_number <= 999 ) 
{
    // match
}

不加preg_*,温馨简单:

$a = array_reverse(explode("/",rtrim($url,"/")));
echo $a[0];
or
$a = array_pop(explode("/",rtrim($url,"/")));
echo $a;

$a中假设完整URL。由于rtrim,即使$url = "http://www.test.com/999/";末尾有斜杠,此代码也可以工作。

This:

preg_match("/(?<=^http:'/'/www''.domain''.com'/string'/999'/string-article-title'/)[1-9][0-9]{0,2}$/", $nlink, $matches);

结果:

Array
(
    [0] => 999
)

regexp可以很容易地扩展,以覆盖请求末尾的任何数字:

/(?<=^http:'/'/www''.domain''.com'/string'/999'/string-article-title'/)[1-9][0-9]*$/