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
Related
This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier
When I use var_dump($_POST), I get this:
array(2) {
["param"]=>
string(327) "{"username":"asd","password":"asdasd","language":"7"}"
}
I need to get username. I've tried it this:
$arr = json_decode($_POST['param'], true);
echo $arr["username"];
But it doesn't work.
Anyone know where my error is and what the right way to get element username is?
Please check the edited answer:
$arr = json_decode($_POST['param'],true);
echo $arr['username'];
Your jason encoded string seems to be inside the param element, Try this:
$arr = json_decode($par['param'], true);
echo $arr["username"];
I am looking for a possibility to pass db table name and column name via php and GET parameter.
I have a data grid with following structure:
table_name1.column1, table_name2.column1, table_name2.column1.
There is a search function for the grid, where I need those parameters.
From the url "?table_name1.column1=22" I am getting through the $_GET only table_name1_column1=22
How would you solve that?
Encode the variable with base64 and decode before you use.
I know that's dirty.
But php variables doesn't support periods (dots)
This is a documented feature of PHP. Its basically because PHP cannot have variable names with dots in them.
$x.y = 1 // is an invalid variable name
MANUAL Convert dots to _ in GET & POST
You can use serialize function of php to pass the text "table_name1.column1" in url. But for that you need to do few coding to get the result. Below I have given code which you can use:
Page1.php
<?php
$test = serialize('table_name1.column1=11::table_name2.column2=22');
?>
<a href='Page2.php?qs=<?php echo $test?>'>test</a>
Use this $test to pass as query string. In above example, I am passing on anchor tag click.
Page2.php
use following code to Unserialize the query string and use the values.
<?php
$test2 = unserialize($_GET['qs']);
$ex = explode('::',$test2);
$new_arr = array();
foreach($ex as $val)
{
$ex2 = explode('=',$val);
$new_arr[$ex2[0]] = $ex2[1];
}
echo $new_arr['table_name1.column1']; //print 11
echo $new_arr['table_name2.column2']; //print 22
?>
Hope this will help you :)
You need to seperate each parameter with &.
So: ?table=table_name1&column=column1
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'];
I'm calling in values using PHP to cURL a site's API. I'm able to pull the data in and put into an array just fine, but when using JSON, one of the attributes ($title) comes back with too much data.
For example, if I just do
echo $new_array[27]['title'];
-> I get "Event Name" but if I do
echo json_encode($new_array[27]['title']);
-> I get {"#attributes":{"abc_id":"8"},"0":"Event Name"}
I want to use JSON as this works with something else I'm doing, but is there a way I can strip out the {"#attributes":{"abc_id":"8"},"0": part leaving just the "Event Name" as a string by itself?
Try:
$json = $new_array[27]['title'];
echo json_encode($json);
I'm not sure what you have in your array there, so these are a guess!
You could try:
unset($new_array[27]['title']['#attributes']);
Or:
$a = array();
foreach($new_array[27]['title'] as $arr) {
$a[] = $arr->__toString();
}
echo json_encode($a);