oliverbj
oliverbj

Reputation: 6052

PHP - Get part of $_GET string

I currently have this:

$thispage = $_SERVER['QUERY_STRING'];

if($thispage == "i=f" || $thispage == "i=f&p=t" ) echo "active";

Although, the "i=f&p=t" is not correct, as the page looks like this:

i=f&p=t&cid=71&id=161

My question is, how can I do so if just the first part of the $_GET string is correct (in this example "i=f&p=t) then it will match $thispage. I don't want it to check for & $_GETs

I want $thispage to work with other pages than just the example above. I only want to check the first part of the string. Not what comes after &

Upvotes: 0

Views: 231

Answers (4)

Mahdi
Mahdi

Reputation: 9417

I'd go for parse_str function:

parse_str($_SERVER['QUERY_STRING'], $thispage);

if ( $thispage['i'] === 'f' OR ($thispage['i'] === 'f' AND $thispage['p'] === 't') ) {
    echo 'active';
}

Update:

I don't want it to check for & $_GETs

I don't know why you don't want to use $_GET if it's the Query String that you're working on that now, but you can do the following as well, which makes more sense:

if ( $_GET['i'] === 'f' OR ($_GET['i'] === 'f' AND $_GET['p'] === 't') ) {
    echo 'active';
}

Upvotes: 1

kunal
kunal

Reputation: 1389

You can access the url parameters directly by using the $_GET array.

var_dump($_GET);

If you want to use $_SERVER['QUERY_STRING'] you can use the parse_str function to parse the query string:

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>

Upvotes: 0

Lionel Chan
Lionel Chan

Reputation: 8069

Did you mean this?

if(strpos($_SERVER['QUERY_STRING'], 'i=f') !== false) {
    echo "active";
}

Upvotes: 1

Brian
Brian

Reputation: 7654

If that is truly what you want to do, then you are actually checking if

if (substr($_SERVER['QUERY_STRING'], 0, 7) === "i=f&p=t")

Upvotes: 0

Related Questions