anamika
anamika

Reputation: 43

How to get everything after first '?' from any url?

I am working on a project and need to store everything after the first ? in url into a new php variable.

I know that it may be done by strpos function but I am little confused and don't know how to do it.

For eg this is my question.

If the url is www.domain.com/abcd?uj=34&kku=oo9 I need to store uj=34&kku=oo9 into a new php variable.

 $id = substr( $url, strrpos( $url, '?' )+1 );

This function does the similar job not exactly it finds the last ?

Upvotes: 0

Views: 1428

Answers (3)

Qaz
Qaz

Reputation: 1576

Try looping over the GET array, like so:

end = "";
foreach($_GET as $key=>$value) {
    end .= $key . "=" . $value . "&";
}

That'll leave you with an extra ampersand at the end, so be sure to trim that off after the foreach loop.

I must add a friendly reminder never to trust input that users submit or can edit. Take care not to use the GET values in a way that could let a hacker do damage or gain access with a crafted URL.

Upvotes: 0

N.Venkatesh Naik
N.Venkatesh Naik

Reputation: 134

$str = explode("?","www.domain.com/abcd?uj=34&kku=oo9");
echo $str[count($str)-1];

Upvotes: 0

Kevin
Kevin

Reputation: 41903

Note: strrpos returns the last occurence, strpos returns the first occurence

You can also use parse_url on this case:

$url = 'www.domain.com/abcd?uj=34&kku=oo9';
$query_string = parse_url($url, PHP_URL_QUERY);
echo $query_string; // uj=34&kku=oo9

Upvotes: 3

Related Questions