Php 检索链接ID
假设我们有如下链接 链接为Php 检索链接ID,php,Php,假设我们有如下链接 链接为**.com/index.php?id=4 为4 链接为**.com/4 为4 链接为**.com/4-keyowrd-keyword $realid = array_shift(explode("-", $id)); 所以是4 现在我的问题是链接**.com/4/关键字 $realid = array_shift(explode("-", $id)); 那么如何获得id为4有什么办法吗 谢谢使用$\u服务器['PHP\u SELF']并分解\以获取变量如果您实际拥
**.com/index.php?id=4
为4
链接为**.com/4
为4
链接为**.com/4-keyowrd-keyword
$realid = array_shift(explode("-", $id));
所以
是4
现在我的问题是链接**.com/4/关键字
$realid = array_shift(explode("-", $id));
那么如何获得id为4有什么办法吗
谢谢使用$\u服务器['PHP\u SELF']
并分解\
以获取变量如果您实际拥有包含方案的完整URL(即:而不仅仅是something.com),您可以执行以下操作:
$realid = array_shift(explode("/", $_SERVER['REQUEST_URI']));
// Search in the query string
$url = 'http://something.php/index.php?id=4';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $queryArr);
$id = $queryArr['id'];
echo $id; // 4
// Search in the path
$url = 'http://something.com/4/keyword-keyword';
$path = parse_url($url, PHP_URL_PATH);
$id = basename(dirname($path));
echo $id; // 4
// Search in the path (undefined length)
$url = 'http://something.com/4/keyword-keyword/foo/bar';
$path = parse_url($url, PHP_URL_PATH);
list(,$id) = explode('/', $path);
echo $id; // 4
如果您不了解该方案,您可以预先编写它,如:
$url = 'http://'.$url;
-我不行。array\u shift接受一个引用,因此您会得到一个E\u STRICT错误。此外,它将始终返回一个空字符串,因为请求URI以斜杠开头。