I have this url:
example.com/?do=search&q=testing+this+out
I need to store the part after &q= in a variable.
What PHP function would be the best for doing that, explode, slice?
P.S. I use $request_url = $_SERVER['REQUEST_URI'];
to get the URI.
$_GET["q"];
Ben 2012-04-04 21:42
If you just need the value of q, you can simply write:
$value = $_GET['q'];
There's parse_url()
to slice 'n dice a url into its components, then parse_str()
will take a query string and decompose it into a regular key-value array.
$url = 'your url here';
$parts = parse_url($url);
$query = parse_str($parts['query']);
echo $query['q'];
I don't understand why $_GET is not sufficient. But incase it isn't, here:
$ php -r '$url = "http://example.com/?do=search&q=testing+this+out"; $parts = parse_url($url); $vars = explode("&", $parts["query"]); print_r($vars);'
Array
(
[0] => do=search
[1] => q=testing+this+out
)
mabye more quick and more simple...
$requested_query_vars = $_SERVER["QUERY_STRING"];
or parse_url() php function
$requested_query_vars = parse_url($request_url, PHP_URL_QUERY);
parse_url()
http://www.php.net/manual/en/function.parse-url.ph - Cfreak 2012-04-04 21:42