坚持使用REQUST_URI解析(PHP)
我有一个页面可以按我的需要工作,最后一个/arist name/解析为正确的变量,但是客户端正在将/artist name/?google tracking=1234fad添加到他们的链接中,这会破坏它 工作 不起作用坚持使用REQUST_URI解析(PHP),php,Php,我有一个页面可以按我的需要工作,最后一个/arist name/解析为正确的变量,但是客户端正在将/artist name/?google tracking=1234fad添加到他们的链接中,这会破坏它 工作 不起作用 $expl = explode("/",$_SERVER["REQUEST_URI"]); $ArtistURL = $expl[count($expl)-1]; $ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL);
$expl = explode("/",$_SERVER["REQUEST_URI"]);
$ArtistURL = $expl[count($expl)-1];
$ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL);
请帮忙,我一直在寻找解决办法。非常感谢 PHP有一个名为
parse_url
的函数,在您尝试使用它之前,它应该为您清理请求uri
解析url
解析URL并返回其组件
例如:
// This
$url_array = parse_url('/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]');
print_r($url_array);
// Outputs this
Array
(
[path] => /artist/pierre-coupey/
[query] => mc_cid=b7e918fce5&mc_eid=[UNIQID]
)
下面是一个演示:
然后,您可以使用路径
块来执行现有逻辑。如果您的所有URL都是
你可以做:
$ArtistURL = preg_replace('/.*\/artist\/(.*)\/.*/','$1',"http://www.odonwagnergallery.com/artist/pierre-coupey/oij");
它将在这方面发挥作用。如果存在其他情况,请指定其他可能的情况。但是@neurommatter answer更通用,+1。如果您只想删除任何和所有查询参数,这一行就足够了:
$url=explode("?",$url)[0];
这会变成
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
进入
但是,如果您想特别删除任何mc_cid
和mc_eid
参数,但要保持url完整:
$url=explode("?",$url);
if(count($url)===2){
parse_str($url[1],$tmp);
unset($tmp['mc_cid']);
unset($tmp['mc_eid']);
$url=$url[0].(empty($tmp)? '':('?'.http_build_query($tmp)));
}else if(count($url)===1){
$url=$url[0];
}else{
throw new \LogicException('malformed url!');
}
这会变成
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
进入
是的,但它也有explode():p