坚持REQUST_URI解析(PHP)

I have a page working as I need it to, with the last /arist-name/ parsing into the correct variable, but the client is adding /artist-name/?google-tracking=1234fad to their links, which is breaking it.

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

http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID] DOES NOT WORK

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

Please help, I have been searching for a solution. Thanks so much!

PHP has a function called parse_url which should clean up the request uri for you before you try to use it.

parse_url

Parse a URL and return its components

http://php.net/parse_url

Example:

// 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]
)

Here is a demo: https://eval.in/873699

Then you can use the path piece to perform your existing logic.

If all your URLs are http://DOMAIN/artist/SOMEARTIST/ you could do:

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

It would work in this context. Specify other possible scenarios if there are others. But @neuromatter answer is more generic, +1.

if you simply want to remove any and all query parameters, this single line would suffice:

$url=explode("?",$url)[0];

this would turn

http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever

into

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

but if you want to specifically remove any mc_cid and mc_eid parameters, but otherwise keep the url intact:

$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!');
}

this would turn

http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever

into

http://www.odonwagnergallery.com/artist/pierre-coupey/?anything_else=whatever