decode url with php - php

how can I decode following query string with php?
t=%B1Z%2B%26k%C9%BF%B1%3Fh%3Fd%3F%9F%2Fa%90%3Ft%C5%8B%A0%3F-%F9s%D5d+%E2sJ-B%9DE%D0T%FA%A4.%93%AF%A05%98d%F9%85%CC%22H%3Fd%F9%9D%C3%22hE%8B%C1%D65%3F%A8%3A%25%24&charset=ISO-8859-1
I've already tried urldecode but I get following output
t=±Z+&kÉ¿±?h?d?Ÿ/a?tÅ‹ ?-ùsÕd âsJ-BEÐTú¤.“¯ 5˜dù…Ì"H?dùÃ"hE‹ÁÖ5?¨:%$&charset=ISO-8859-1
I've tried many ways but couldn't decode it
thanks in advance

Use parse_str to "decode" and parse your string, as in the example snippet further down in this post.
Read more about the function in php.net's manual:
PHP: parse_str - Manual
$data = "t=%B1Z%2B%26k%C9%BF%B1%3Fh%3Fd%3F%9F%2Fa%90%3Ft%C5%8B%A0%3F-%F9s%D5d+%E2sJ-B%9DE%D0T%FA%A4.%93%AF%A05%98d%F9%85%CC%22H%3Fd%F9%9D%C3%22hE%8B%C1%D65%3F%A8%3A%25%24&charset=ISO-8859-1";
parse_str ($data, $out);
print_r ($out);
Array
(
[t] => ±Z+&kÉ¿±?h?d?/a?tÅ ?-ùsÕd âsJ-BEÐTú¤.¯ 5dùÌ\"H?dùÃ\"hEÁÖ5?¨:%$
[charset] => ISO-8859-1
)

Read about urlencode and urldecode here
$output = urldecode($encoded);

Use urldecode!
$decoded = urldecode(substr($queryString,3));

maybe this one might help:
<?php
$variable = 'http%3A%2F%2Fsample.com';
$variable = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($variable));
$variable = html_entity_decode($variable,null,'UTF-8');
echo $variable;
?>
output will be : http://sample.com

Related

Convert the following array with index to a string

I have an array output with index as:
[{"0":"10:00PM","2":"12:00PM"}]
I want to convert this to a string something like:
[{"10:00PM","12:00PM"}]
How can I do this in PHP?
You can use preg_replace to replace string for following output.
$json = '[{"0":"10:00PM","2":"12:00PM"}]';
$result = preg_replace("/\"\d\"\:/","",$json);
echo $result;
Output
[{"10:00PM","12:00PM"}]
Live demo
Know more about preg_replace
You can just push your value into an array.
$data = array();
array_push($data,"10:00PM");
array_push($data,"12:00PM");
echo json_encode($data);
Output:
[{"10:00PM","12:00PM"}]

How convert $GLOBALS to string?

I need save $GLOBALS into a field in MySQL, but...
$GLOBALS is an ARRAY
When I try with some function as
function array_to_string($array){
$string = '';
code...
$string .= code...
code...
return $string;
}
$string = array_to_string($GLOBALS);
this "$string" grow and grow... (is infinite)
Any idea please?
you can try implode() function....
The implode() function returns a string from the elements of an array.
For example..
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
the output
Hello World! Beautiful Day!
Conversion to JSON is adviced.
$string=json_encode($array);
json_encode Returns a JSON encoded string on success or FALSE on failure.
$array_back=json_decode($string);
json_decode Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
Try to use utf8_encode and json_encode
$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);

Extracting data from Json in Php

I have this Json Object Below, I want to extract this data and output it in PHP
{"seat_booked":"A5","0":"A5","1":"A3"}
then get them into this format
$seat_booked = "'A5', 'A5', 'A3'";
How can I do this?
I hope you are looking for this, its very simple example by using json_decode():
$string = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = json_decode($string,true);
$resuiredString = '"'."'".implode("','", $decoded)."'".'"';
echo $resuiredString;
Result:
"'A5','A5','A3'"
Side Note:
I suggest you to learn about variable concatenation.
PHP Concatenation
Another solution:
$json = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = array_map(
function($val) {
return "'". $val."'";
},
array_values(json_decode($json, true))
);
To get an object from a json in php you can use json_decode has explained here.
But you have another problem, your json is wrong!
If you want to represent a single dimensional array you should at least do this
["A5","A5","A3"]
Finally, using json_decode:
$obj = json_decode('["A5","A5","A3"]');
var_dump($obj);
Also, you could do something like:
{"0":"A5","1":"A5","2":"A3"}
$obj = json_decode('{"0":"A5","1":"A3", "2": "A5"}', true);
var_dump($obj);
Edit:
It's not very clear from your question if you are trying to get back an object from a json or if you just want to get a string from it.
If what you need is an string then you don't even need json, you could do this by string manipulation and/or using regex.
But just for completeness, if a quoted comma separated string is what you need you can do this:
$array = json_decode('["A5","A5","A3"]');
$str = implode("','",$array);
$str = "'" . $str . "'";
var_dump($str);

convert url string to array php

I pass with jquery ajax as data array the following structure
$_POST['data'][0] = 'results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein'
$_POST['data'][1] = '9b2c1230757e4354b384c5c93e8e8f26'
How do I say to php to interpret $_POST['data'][0] as array. What I would like to get is array(1 =>'stein', 2=>'schere'...)
Use parse_str() — Parses the string into variables
$str = "results[]=stein&results[]=schere&results[]=stein&results[]=schere&results[]=stein";
parse_str($str, $output);
echo $output['results'][0]; // stein
Live CodePad
use parse_str()..
parse_str($_POST['data'][0]);
print_r($results);
echo $results[0]; //stein;
echo $results[1]; //schere;

how to parse $_REQUEST?

here i am checking for and getting part of the request, (the ids). i also print out the entire request:
if (isset($_REQUEST['ids'])){
$amount = sizeof($_REQUEST['ids']);
print_r($_REQUEST);
echo "<br>$amount invitations Successfully Sent";
}
this is how the entire $_REQUEST prints out:
Array
(
[mfs_typeahead_req_form_4cf74f96db3688507476560] => Start Typing a Name
[ids] => Array
(
[0] => 510149460
)
[fbs_258181898823] => \"access_token=258181898823|2.q_qb_yoReO0_xc4H8PxKRQ__.3600.1291280400-100000664203700|LqtGr_OiJTASGmek61awxmxfvFk&expires=1291280400&secret=85eTEELZj8lkV82V_PwRSA__&session_key=2.q_qb_yoReO0_xc4H8PxKRQ__.3600.1291280400-100000664203700&sig=d4cc0e4c0992ea29c0adfd60dc27185b&uid=100000664203700\"
)
i need to parse the part at the end: &uid=100000664203700, specifically '100000664203700'
$queryString = $_REQUEST["fbs_258181898823"];
$output = array();
parse_str($queryString, $output);
print $output["uid"];
parse_str() will break up things that look like a query string. I recommend you pass an array as the second parameter.
You can use parse_str()—it should do the trick.
First, remove the \" (using a combination of preg_replace and stripslashes) at the beginning and the end, then use parse_str:
$str = preg_replace('/(^"|"$)/', '', stripslashes($_REQUEST['fbs_258181898823']));
parse_str($str, $data);
$uid = $data['uid'];

Categories