PHP从带有url的变量中获取参数

PHP get parameter from variable with url

本文关键字:变量 获取 参数 url PHP      更新时间:2023-09-26

我想从保存URL的变量中获取参数
我得到了以下变量:$lastPage,它保存上一页的URI。

我想从中获取一个参数。
我用javascript(用于其他用途)获得了以下脚本

function getUrlVar(key)
{
    var result = new RegExp(key + '=([^&]*)', 'i').exec(window.location.search);
    return result && unescape(result[1]) || '';
}

我想要这样的东西,但对于PHP。

非常感谢

$url = 'http://php.net/manual/en/function.parse-url.php?id=my_awesome_id';
$arr = parse_url($url);
parse_str($arr['query']);
echo $id; // my_awesome_id

参考:

parse_str

parse_url

您可以在php中使用preg_match()preg_match_all(),这将为您提供正则表达式功能。

http://php.net/manual/en/function.preg-match.php