BENN1TH
BENN1TH

Reputation: 2023

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
}

Upvotes: 2

Views: 418

Answers (1)

Saty
Saty

Reputation: 22532

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";
}

Upvotes: 4

Related Questions