2017-10-04 49 views
1

我有一個頁面按我需要的方式工作,最後/ arist-name /解析爲正確的變量,但客戶端正在添加/ artist-name /?google-tracking = 1234發現他們的聯繫,這是打破它。用REQUST_URI解析(PHP)

http://www.odonwagnergallery.com/artist/pierre-coupey/ WORKS

http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]不起作用

$expl = explode("/",$_SERVER["REQUEST_URI"]); 
$ArtistURL = $expl[count($expl)-1]; 
$ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL); 

請幫幫忙,我一直在尋找解決的辦法。非常感謝!

回答

3

PHP有一個叫做parse_url的函數,它應該在您嘗試使用它之前清理請求的uri。

parse_url

解析URL,返回其組成部分

http://php.net/parse_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] 
) 

這裏是一個演示:https://eval.in/873699

然後,您可以使用path一塊執行您現有的邏輯。

+0

真實的,但它也有爆炸():P – hanshenrik

0

如果你的所有網址都http://DOMAIN/artist/SOMEARTIST/ 你可以這樣做:

$ArtistURL = preg_replace('/.*\/artist\/(.*)\/.*/','$1',"http://www.odonwagnergallery.com/artist/pierre-coupey/oij"); 

它會在這方面的工作。指定其他可能的情況,如果有其他情況。但@neuromatter的答案更通用,+1。

0

,如果你只是想刪除任何和所有的查詢參數,這一行就足夠了:

$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_cidmc_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/?anything_else=whatever