从网址获取数字的最简单方法是什么?

时间:2011-04-17 21:38:06

标签: php regex preg-replace preg-match

我希望有人可以帮助我,我已经创建了这样的网址

/this/is/a/test/index.asp
/this/is/a/test/index-1.asp
/this/is/a/test/index-2.asp
/this/is/a/test/index-3.asp

从网址中删除号码的最简单方法是什么?

而不是使用这样的变量:

/this/is/a/test/index.asp?no=1
/this/is/a/test/index.asp?no=2
/this/is/a/test/index.asp?no=3

创建变量我正在使用URL中的数字来动态调用页面上的内容。

如果网址为:/this/is/a/test/index-3.asp,则应使用3并根据其匹配内容,就像我打电话一样

  

?没有= 3

我正在使用PHP ...

url结构将始终将变量define定义为匹配最后一个' - '[the-number]'.asp'

由于

Gerald Ferreira

2 个答案:

答案 0 :(得分:3)

您可以使用mod_rewrite将网址格式映射到实际网址。您可以使用类似于以下内容的方式实现您想要的目标:

RewriteRule ^index-([0-9]+).asp$ index.asp?no=$1

答案 1 :(得分:1)

你应该能够将它与:

匹配
if (preg_match('/\-(\d+)\.asp$/', $_SERVER['REQUEST_URI'], $a)) {
    $pageNumber = $a[1];
} else {
    // failed to match number from URL
}
相关问题