Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从url获取数字的最简单方法是什么?_Php_Regex_Preg Replace_Preg Match - Fatal编程技术网

Php 从url获取数字的最简单方法是什么?

Php 从url获取数字的最简单方法是什么?,php,regex,preg-replace,preg-match,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 从URL中删除数字的最简单方法是什么 不要使用这样的变量: /this/is/a/test/index.asp?no=1 /this/is/a/test/index.asp?no=2 /this/is/a/test/index.asp

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

/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
从URL中删除数字的最简单方法是什么

不要使用这样的变量:

/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中的数字动态调用页面上的内容

如果url是:
/this/is/a/test/index-3.asp
它应该使用3并根据它匹配内容,就像我要调用

?否=3

我用PHP来做这个

url结构将始终将变量定义为与最后一个'-'[数字]'匹配。asp'

谢谢

Gerald Ferreira可以用来将URL模式映射到实际URL。您可以通过以下类似的方式实现您想要的:

RewriteRule ^index-([0-9]+).asp$ index.asp?no=$1
您可以使用将URL模式映射到实际URL。您可以通过以下类似的方式实现您想要的:

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

您应该能够将其与以下内容相匹配:

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

您应该能够将其与以下内容相匹配:

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

GET参数是URL的一部分。那你为什么要这么做呢?嗨,哈夫丹,因为如果谷歌点击我的页面“index-1.asp”,那么它将显示与index.asp相同的内容,尽管它实际上是不同的。如果我是对的,您将需要按照链接获得url来传递值。我正在创建干净的url,但已经在使用htaccess,在这种情况下它将无法帮助我。GET参数是url的一部分。那你为什么要这么做呢?嗨,哈夫丹,因为如果谷歌点击我的页面“index-1.asp”,那么它将显示与index.asp相同的内容,尽管它实际上是不同的。如果我是对的,您将需要按照链接获得url来传递值。我正在创建干净的url,但已经在使用htaccess了——在这种情况下它将无法帮助我。嗨,尼克——我已经在使用mod_rewrite来构建页面,我不认为我能够在这种特殊情况下使用它。嗨,尼克——也许只是为了略加说明。我应该在上面的问题中提到它,但指数不一定是常数。因此,我不能真正使用它,因为它变化很大,可能会与我已有的重写规则冲突Hi-Nick-我已经在使用mod_rewrite来构建页面,我不认为我能在这个特殊情况下使用它。Hi-Nick-也许只是稍微简化一下。我应该在上面的问题中提到它,但指数不一定是常数。因此,我不能真正使用它,因为它变化很大,很可能会与我已经有了很好的重写规则相冲突,感谢詹姆斯,正是我所寻找的。。。你能告诉我“d”是什么意思吗regex@Gerald:The
\d
是小数的缩写。很好的一个,谢谢James,这正是我要找的。。。你能告诉我“d”是什么意思吗regex@Gerald:d是小数的缩写。