0

PHP 去掉url指定参数

tony created at5 years ago view count: 3191
/**
 * 去掉URL中指定的参数, 删除无用参数
 *
 * @param mixed $paramNames.
 * @param string $url.
 *
 * @return string
 */
function removeUrlParam($paramNames, $url) {
    if (is_string($paramNames)) {
        $paramNames = array($paramNames);
    }
    $baseUrl = $url;
    $arr = array();
    if (strpos($url, "?") !== false) {
        $urlInfo = explode("?", $url);
        $baseUrl = $urlInfo[1];
        parse_str($baseUrl, $arr);
    }
    foreach ($arr as $name => $v) {
        if (in_array($name, $paramNames)) {
            unset($arr[$name]);
        }
    }
    return $urlInfo[0]."?".http_build_query($arr);
}

echo removeUrlParam('ut_sk', 'https://detail.tmall.com/item.htm?id=544678718923&ut_sk=1');
report
回复

Recent search keywords