简体   繁体   中英

Check in PHP if a specific query is in URL

I am trying to check if a URL has a specific query within a URL with multiple parameters:

$parts = parse_url('http://my url?queryA=valueA&queryB=valueB&queryC=valueC ....');
parse_str($parts['query'], $query);

I have used the below to get the value of the query;

$query['queryA']//outputs valueA

However, I want to check if "queryA" is indeed within the URL queries:

echo $parts['query'] // Outputs the full query - queryA=valueA&queryB=valueB&queryC=valueC

// Trying for
if($parts['query'] == 'queryA') {
    // queryA is in the URL
}

Use array_key_exists to check queryA key present in $query array

$parts = parse_url('http://my url?queryA=valueA&queryB=valueB&queryC=valueC');
parse_str($parts['query'], $query);
if (array_key_exists('queryA', $query)) {
    echo "queryA element is in the array";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM