Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the GET variables from a URL String

Tags:

variables

php

get

Hey, say I have a url just being passed through my php is there any easy way to get some GET variables that are being passed through it? It's not the actual url of the page or anything.

like a just have a string containing

http://www.somesite.com/index.php?url=var&file_id=var&test=var

Whats the best way to get the values for those variables?

like image 683
Belgin Fish Avatar asked Jun 28 '10 21:06

Belgin Fish


People also ask

How can I get value from GET request URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

What is $_ GET?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html>


2 Answers

parse_str(parse_url($url, PHP_URL_QUERY), $array), see the manpage for parse_str for more info.

like image 81
cristis Avatar answered Sep 22 '22 02:09

cristis


$href = 'http://www.somesite.com/index.php?url=var&file_id=var&test=var';

$url = parse_url($href);
print_r($url);
/* Array
(
    [scheme] => http
    [host] => www.somesite.com
    [path] => /index.php
    [query] => url=var&file_id=var&test=var
) */

$query = array();
parse_str($url['query'], $query);

print_r($query);
/* Array
(
    [url] => var
    [file_id] => var
    [test] => var
) */
like image 35
Kris Avatar answered Sep 21 '22 02:09

Kris