6

我想从 URL 中删除一个参数:

$linkExample1='https://stackoverflow.com/?name=alaa&counter=1';
$linkExample2='https://stackoverflow.com/?counter=4&star=5';

我试图得到这个结果:

我正在尝试使用它来完成它preg_replace,但我不知道它是如何完成的。

4

8 回答 8

8
$link = preg_replace('~(\?|&)counter=[^&]*~','$1',$link);
于 2012-06-11T18:20:24.413 回答
3

Relying on regular expressions can screw things up sometimes..

You should use, the parse_url() function which breaks up the entire URL and presents it to you as an associative array.

Once you have that array, you can edit it as you wish and remove parameters.

Once, completed, use the http_build_url() function to rebuild the URL with the changes made.

Check the docs here..

Parse_Url Http_build_query()

EDIT

Whoops, forgot to mention. After you get the parameter string, youll obviously need to separate the parameters as individual ones. For this you can supply the string as input to the parse_str() function.

You can even use explode() with & as the delimeter to get this done.

于 2012-06-11T18:24:25.117 回答
1

I would recommend using a combination of parse_url() and http_build_query().

于 2012-06-11T18:24:16.450 回答
1

一个代码示例,我将如何获取请求的 URL 并删除一个名为“name”的参数,然后重新加载页面:

$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //complete url
$parts = parse_url($url);
parse_str($parts['query'], $query); //grab the query part
unset($query['name']); //remove a parameter from query
$dest_query = http_build_query($query); //rebuild new query
$dest_url=(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http").'://'.$parts['path'].'?'.$dest_query; //add query to host
header("Location: ".$dest_url); //reload page
于 2017-03-10T13:06:59.747 回答
1

正确处理!!

remove_query('http://example.com/?a=valueWith**&**inside&b=value');

代码:

function remove_query($url, $which_argument=false){ 
    return preg_replace( '/'. ($which_argument ? '(\&|)'.$which_argument.'(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)' : '(\?.*)').'/i' , '', $url);  
}
于 2017-11-29T11:12:24.150 回答
0

parse_url()并且parse_str()是越野车。正则表达式可以工作,但有破坏的趋势。如果您想正确解构、进行更改然后重构 URL,您应该查看:

http://barebonescms.com/documentation/ultimate_web_scraper_toolkit/

ExtractURL()生成parse_url()类似的输出,但做得更多(并且做得对)。 CondenseURL()从信息中获取一个数组ExtractURL()并构造一个新的 URL。这两个函数都在“support/http.php”文件中。

于 2012-06-11T18:52:20.040 回答
0

这对我有用:

function removeParameterFromUrl($url, $key)
{
    $parsed = parse_url($url);
    $path = $parsed['path'];
    unset($_GET[$key]);
    if(!empty(http_build_query($_GET))){
      return $path .'?'. http_build_query($_GET);
    } else return $path;
}
于 2017-11-16T02:02:07.853 回答
0

多年后... $_GET可以像 PHP 中的任何其他数组一样被操作。只需取消设置密钥并使用http_build_query函数创建 http 查询。

// Populate _GET with sample data...
$_GET = array(
    'value_a' => "A",
    'key_to_remove' => "Don't delete me bro!",
    'value_b' => "B"
);
// Should output everything...
// "value_a=A&key_to_remove=Don%27t+delete+me+bro%21&value_b=B"
echo "\n".http_build_query( $_GET );

// Remove the key from _GET...
unset( $_GET[ 'key_to_remove' ] );
// Should output everything else...
// "value_a=A&value_b=B"
echo "\n".http_build_query( $_GET );
于 2017-08-02T19:10:13.500 回答