getting variables from STRING url in php - php

If I have a url that looks like this, what's the best way to read the value
http://www.domain.com/compute?value=2838
I tried parse_url() but it gives me value=2838 not 2838
Edit: please note I'm talking about a string, not an actual url. I have the url stored in a string.

You can use parse_url and then parse_str on the query.
<?php
$url = "http://www.domain.com/compute?value=2838";
$query = parse_url($url, PHP_URL_QUERY);
$vars = array();
parse_str($query, $vars);
print_r($vars);
?>
Prints:
Array
(
[value] => 2838
)

For http://www.domain.com/compute?value=2838 you would use $_GET['value'] to return 2838

$uri = parse_url($uri);
parse_str($uri['query'], $params = array());
Be careful if you use parse_str() without a second parameter. This may overwrite variables in your script!

parse_str($url) will give you $value = 2838.
See http://php.net/manual/en/function.parse-str.php

You should use GET method e.g
echo "value = ".$_GET['value'];

Related

PHP - How to get $_GET Parameter without value

I need to know how to get the $_GET parameter from requested url. If I use for example
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
echo $parse_url; will output query=1, but I need only the parameter not parameter AND value to check if parameter is in array.
Based on your example, simply exploding the = might quickly suits your need.
$url = $_SERVER['REQUEST_URI'];
$parse_url = parse_url($url, PHP_URL_QUERY);
$queryparam = explode("=", $parse_url);
echo $queryparam[0];
/* OUTPUT query */
if (in_array($queryparam[0], $array_of_params)){ ... }
But you can simply achieve the same thing like this:
if (#$_GET["query"] != ""){ ... }
Something like:
// Example URL: http://somewhere.org?id=42&name=bob
// An array to store the parameters
$params = array();
parse_str($_SERVER['QUERY_STRING'], $params);
// Access what you need like this:
echo 'ID: ' .$params['id'];
The Arry $_GET contains all needed informations. $ _SERVER ['REQUEST_URI'] and parse_url not needed.
As an example I have the following browser line:
http://localhost/php/test.php?par&query=1
Let's see what $ _GET contains
echo '<pre>';
var_export($_GET);
Output
array (
'par' => '',
'query' => '1',
)
With array_keys() I get all keys.
$keys = array_keys($_GET);
var_export($keys);
Output:
array (
0 => 'par',
1 => 'query',
)
The first key I get with (see also comment from #bobble bubble)
echo $keys[0]; //par
or
echo key($_GET);
With array_key_exists () I can check whether a key is available.
if(array_key_exists('query',$_GET)){
echo 'query exists';
}
You can use basename() too, it Returns trailing name component of path. For example:
$lastPart=explode("=",basename('http://yourdomain.com/path/query=1'));
echo $lastPart[0];
Output
query

{PHP} Split text string

So I am probably making api system and I am trying to get the client req api.
I checked some tutorials and I found I could use :
$data = $_SERVER['QUERY_STRING'];
It's works but I am getting string like : action=get&id=theapikeygoeshere.
And I need to get only the text after id= , How can I do that?
Thanks!
parse the query string using parse_str:
<?php
$arr = array();
parse_str('action=get&id=theapikeygoeshere', $arr);
print_r($arr);
?>
This gives:
Array
(
[action] => get
[id] => theapikeygoeshere
)
I think the best thing is to use $_GET['id'] but if you want to extract any thing from the QUERY_STRING use
parse_str($_SERVER["QUERY_STRING"], $output);
var_dump($output["id"]);
You can do so by using $_GET['id']. :) $_GET can be used for any URL parameters like those.
E.g.:
$info = $_GET['id']

PHP get a Part of the URL

I want to fetch the value of until from following url,
https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ
Thanks
You can use parse_str():
$url = 'https://graph.facebook.com/v2.1/168820059842589/feed?fields=id,from,message,comments%7Bfrom,id,created_time,message,attachment,comments%7Bfrom,id,created_time,message,attachment%7D%7D,type,created_time,updated_time,picture,name,caption,link,source,description,likes,story,place,story_tags&limit=250&include_hidden=true&until=1394190620&__paging_token=enc_AeyIp_kAzWE_u3avDbSjlT69EtTXCEOSDLyp0xSOTptJHfmlHKARfuTm197SkZAPDhKsXAtfu9G7jqzn75ymXK60UwG3nR2itFLk-IhY_hdOzQ';
$arr = array();
parse_str($url,$arr);
echo $arr['until'];
//outputs 1394190620

Explode string into foreach

I have the following GET parameters:
?data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127
I was wondering if anybody knew a quick method of getting each value into a foreach? Thanks!
This won't work, because all of your parameters have the same name, so only the value "prop_127" is going to be passed to your script. You can create this as an array instead, like this:
?data[]=prop_123&data[]=prop_124&data[]=prop_125&data[]=prop_126&data[]=prop_129&data[]=prop_127
And then you can use foreach() to loop through them like this:
foreach ( $_GET['data'] as $data ) {
// This loops once for each instance of data[] in your URL
}
If you have no control over the URL, you'd have to read it in manually with $_SERVER['QUERY_STRING'], then parse through it to pull the values out.
Each data will overwrite the previous in $_GET or with parse_str so something like this maybe:
$s = 'data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127';
$s = str_replace('=', '[]=', $s);
parse_str($s, $a);
foreach($a as $something) { }
If you are actually trying to put the vars in a GET request and can get them from $_GET, then use the form data[] in the query string and then just foreach over $_GET.
You can use the PHP function: parse_str
http://www.php.net/manual/en/function.parse-str.php

PHP: parsed_str help please

This snippet works fine:
$url="Http://www.youtube.com/watch?v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4";
$parsed_url=parse_url($url);
echo "<br><br><pre>";
print_r(parse_url($url));
echo "</pre>";
echo $parsed_url['query'];
But when I add the below:
echo "<br><br><pre>";
$parsed_str=parse_str($parsed_url['query']);
print_r($parsed_str);
echo "</pre>";
Nothing happens. I suspect parse_str() isn't working as it should. Any ideas what I might be doing wrong?
If you want to have the result of parse_str() in an array, pass the array as the second argument:
parse_str($parsed_url['query'], $parsed_str);
var_dump($parsed_str);
I'm going to assume that the user inputs the URL. If so, do not use parse_str without a second argument!. Doing so would result in a security risk where the user can overwrite arbitrary variables with the value of their choice.
parse_str() doesn't return anything. It populates variables.
For example, if you have a query string of $query = "param=1&test=2"
after
parse_str($query);
you can check var_dump($param) and var_dump($test) - those two variables would be created for you.
Basically, parse_str converts
v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4
To
$v = "upenR6n7xWY"
$feature = "BFa"
$list = "PL88ACC6CB00DC2B44"
$index = 4

Categories