如何删除网址上的参数

时间:2015-01-24 15:11:37

标签: php url

我有这个网址 [HTTP_REFERER] => http://localhost/mysystem/my-system/?page=Page1

我希望将其转换为 http://localhost/mysystem/my-system/

我目前正在使用这个:

    $parse = parse_url($_SERVER['HTTP_REFERER']);
    $path = http_build_url($_SERVER['HTTP_REFERER'],
        array(
            "scheme" => $parse['scheme'],
            "host" => $parse['host'],
            "path" => $parse['path'],
            "query" => " "
        ),
        HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
    );

给我这个错误:致命错误:调用未定义的函数http_build_url()。 有没有其他方法可以做到这一点,而无需设置http_build_url()?

1 个答案:

答案 0 :(得分:0)

试试这个:

function getURLWithoutParams($url) {
    //Getting the position of ? as it is the start of the parameters
    $position = strpos($url, "?");
    //strpos returns false if the search yielded no results. If it is not
    //false, then we need to extract the needed part, as the url has parameters
    if ($position !== false) {
        return substr($url, 0, strpos($url, "?"));
    }
    return $url;
}

使用getURLWithoutParams($_SERVER['HTTP_REFERER'])

进行调用