我想使用 preg_expression 从 URL 中删除所有出现的特定参数模式。如果存在,还删除最后一个“&” 模式如下:make=xy(“make”是固定的;“xy”可以是任意两个字母)
例子:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
处理完preg_replace
,结果应该是:
http://example.com/index.php?c=y&do=ms&r=k&p=7
我尝试使用:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
但是,这并没有很好地工作,因为我的 URL 中有 make=xx 的重复项(这种情况可能发生在我的应用程序中)。
最佳答案
你不需要正则表达式:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
关于PHP preg_replace 表达式删除 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783116/