【问题标题】:PHP get parameter from variable with urlPHP从带有url的变量中获取参数
【发布时间】:2012-07-13 05:28:48
【问题描述】:

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

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

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

我想要类似的东西,但对于 PHP。

非常感谢

【问题讨论】:

    标签: php javascript url-parameters


    【解决方案1】:
    $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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-05
        • 2014-02-02
        • 1970-01-01
        • 2019-05-21
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        相关资源
        最近更新 更多