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;
Related
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"}]
I have the json data in following format
["0","0","0","0","0","0","0","2","5","4","3","0"]
I want to convert the above data in to following format using php
[0,0,0,0,0,0,0,2,5,4,3,0]
How can i do this using php
Thanks
You can use array_map to typecast all items in the array to an integer with the callback intval.
For integers in an array:
$array = array_map('intval', json_decode('["0","0","0","0","0","0","0","2","5","4","3","0"]'));
Retrieve JSON from array:
echo json_encode($array);
$string = '["0","0","0","0","0","0","0","2","5","4","3","0"]';
echo str_replace('"','', $string);
Output is:
[0,0,0,0,0,0,0,2,5,4,3,0]
I'm searching a function to cut the following string and get all content BEFORE and AFTER
I need this part<!-- more -->and also this part
Result should be
$result[0] = "I need this part"
$result[1] = "and also this part"
Appreciate any help!
Use the explode() function in PHP like this:
$string = "I need this part<!-- more -->and the other part.
$result = explode('<!-- more -->`, $string) // 1st = needle -> 2nd = string
Then you call your result:
echo $result[0]; // Echoes: I need that part
echo $result[1]; // Echoes: and the other part.
You can do this pretty easily with regular expressions. Somebody out there is probably crying for parsing HTML/XML with regular expressions, but without much context, I'm going to give you the best that I've got:
$data = 'I need this part<!-- more -->and also this part';
$result = array();
preg_match('/^(.+?)<!--.+?-->(.+)$/', $data, $result);
echo $result[1]; // I need this part
echo $result[2]; // and also this part
If you are parsing HTML, considering reading about parsing HTML in PHP.
Use preg_split. Maybe something like this:
<?php
$result = preg_split("/<!--.+?-->/", "I need this part<!-- more -->and also this part");
print_r($result);
?>
Outputs:
Array
(
[0] => I need this part
[1] => and also this part
)
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
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'];