0

我必须在下面过滤此 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:对不起我的英语不好

4

5 回答 5

2

你可能会得到999

$ret = array_pop(explode('/', $nlink));
于 2013-04-28T14:28:45.793 回答
0
$page_number = substr( $url, strrpos($url, "/") + 1 ); // returns 999
if ( $page_number >= 1 && $page_number <= 999 ) 
{
    // match
}
于 2013-04-28T14:30:44.083 回答
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]*$/
于 2013-04-28T14:28:14.527 回答
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
        )

)
于 2013-04-28T14:28:52.133 回答
0

没有preg_*,甜蜜而简单:

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

or

$a = array_pop(explode("/",rtrim($url,"/")));
echo $a;

完整的 URL 假定在$a. 即使$url = "http://www.test.com/999/";最后带有斜杠,此代码也将起作用,因为rtrim.

于 2013-04-28T14:36:41.027 回答