jQuery unserialize data serialized by PHP - php

How can I unserialize data in jQuery? remember data is Serialized by PHP. Below is given example.
a:2:{i:0;s:9:" img1.jpeg";i:1;s:9:"img2.jpeg";}

This can be achieved using unserialize and json_encode
$unserialized = unserialize($serialized_from_db);
echo json_encode($unserialized);
But please note your sample provided: s:9:" img1.jpeg" this part is incorrect. The s:9 means it expects string to be 9 bytes (this link provides a good guide on understanding output from serialize), however " img1.jpeg has a space and therefore is 10 bytes and fails: demo failing. You can add a check to see if this failes to unserialize:
if ($unserialized === false)
When you fix the incorrect part to: s:9:"img1.jpeg, giving you:
a:2:{i:0;s:9:"img1.jpeg";i:1;s:9:"img2.jpeg";}
will now work, see demo.
At the end of the day I would recommend as others have above, to store json_encoded values in your database rather than serialized values.
I came up with a really hacky hack to potentially fix bad serialized data:
<?php
function hackFixUnserialized($unserialized_string) {
$parts = explode(';', $unserialized_string);
foreach ($parts as &$part) {
$kv = explode(':', $part);
if ($kv[0] == 's') {
$str_without_quotes = str_replace('"', '', $kv[2]);
if ($kv[1] != strlen($str_without_quotes)) {
$kv[1] = strlen($str_without_quotes);
}
}
$part = implode(':', $kv);
}
return implode(';', $parts);
}
$unserialized_from_db = <<<EOT
a:2:{i:0;s:9:" img1.jpeg";i:1;s:9:"img2.jpeg";}
EOT;
$unserialized = unserialize($unserialized_from_db);
if ($unserialized === false) {
$hack_fix = hackFixUnserialized($unserialized_from_db);
printf('bad unserialized, fixed to: %s%s', $hack_fix, PHP_EOL);
$unserialized = unserialize($hack_fix);
}
echo json_encode($unserialized);
Demo of it here: https://eval.in/783408
Hope this helps

Related

Warning: substr_count(): Empty substring

I'm suddenly getting an awful lot of errors saying "Empty substring" referring to line 8
$score3 = substr_count($name_only, $text);
I have no idea what the issue is, this is a search function. Is it empty submission into the search box?
I thought it might be so I made changes in with JS and HTML so isn't possible to submit the search form blank or with just whitespace, but still the error continues.
This is my php, does anything stand out as the source of the issue to anyone with better knowledge than I have?
function search_now($images){
global $text, $scores, $scores2, $scores3, $do_search;
$images2 = array();
foreach ($images as $key => $value) {
$name_only = $value['name'];
similar_text($text, $name_only, $score);
$score2 = substr_compare($name_only, $text, 0);
$score3 = substr_count($name_only, $text);
if($do_search){
if($score<20)
continue;
}
$images2[$key] = $value;
$images2[$key]['score'] = $score;
$images2[$key]['score2'] = $score2;
$images2[$key]['score3'] = $score3;
//$scores[$key] = ($do_search)? $score : $key;
$scores[$key] = $score;
$scores2[$key] = $score2;
$scores3[$key] = $score3;
}
return $images2;
}
That error message is triggered when the second argument for substr_count() is an empty string. If you dump $text I imagine you will find it's an empty string.
Not sure how your snippet relates to the rest of your code but you could include a check in your function...
if ($text == '') {
// handle scoring differently
}

Convert a semicolon separated string to an array of associative arrays then json encode

I am facing some problems while converting a set of semicolon delimited strings to json.
The input string:
si;dialed_no;connect_time;duration;region;call_cost
0;918592877727;2015-08-25 18:51:01;21;India(91);0.029
1;918907777727;2015-08-25 19:04:08;220;India(91);0.232
2;918907777727;2015-08-25 19:09:50;40;India(91);0.058
3;918907777727;2015-08-25 19:10:46;69;India(91);0.087
4;919048232151;2015-08-26 13:30:24;19;India(91);0.029
5;919895842822;2015-08-26 14:23:35;423;India(91);0.435
My code:
function my_wrap($val) {
return '{"test":"' . $val. '"}';
}
$parts = explode(';', $string);
$parts = array_map('my_wrap', $parts);
$json = '[' . implode(',', $parts) . ']';
echo $json;
And the output is like:
[{"test":"dialed_no"},{"test":"connect_time"},{"test":"duration"},{"test":"region"},{"test":"call_cost 0"},{"test":"918592877727"},{"test":"2015-08-25 18:51:01"},{"test":"21"},{"test":"India(91)"},{"test":"0.029 1"},{"test":"918907777727"},{"test":"2015-08-25 19:04:08"},{"test":"220"},{"test":"India(91)"},{"test":"0.232 2"},{"test":"918907777727"},{"test":"2015-08-25 19:09:50"},{"test":"40"},{"test":"India(91)"},{"test":"0.058 3"},{"test":"918907777727"},{"test":"2015-08-25 19:10:46"},{"test":"69"},{"test":"India(91)"},{"test":"0.087 4"},{"test":"919048232151"},{"test":"2015-08-26 13:30:24"},{"test":"19"},{"test":"India(91)"},{"test":"0.029 5"},{"test":"919895842822"},{"test":"2015-08-26 14:23:35"},{"test":"423"},{"test":"India(91)"},{"test":"0.435 6"},{"test":"8801711788025"},{"test":"2015-08-30 19:29:48"},{"test":"1"},{"test":"Bangladesh(880)"},{"test":"0.029 7"},{"test":"8801711788025"},{"test":"2015-08-30 19:29:57"},{"test":"2"},{"test":"Bangladesh(880)"},{"test":"0.029 8"},{"test":"8801711788025"},{"test":"2015-08-30 19:30:07"},{"test":"2"},{"test":"Bangladesh(880)"},{"test":"0.029 9"},{"test":"8801711788025"},{"test":"2015-08-30 19:30:17"},{"test":"1"},{"test":"Bangladesh(880)"},{"test":"0.029 10"},{"test":"8801711788025"},{"test":"2015-08-30 21:24:31"},{"test":"88"},{"test":"Bangladesh(880)"},{"test":"0.087 11"},{"test":"8801833316038"},{"test":"2015-08-31 12:06:15"},{"test":"5"},{"test":"Bangladesh(880)"},{"test":"0.029 12"}]
What I want is like:
[{si:"0",dialed_no:"91xxx",connect_time:"2015-08-25 18:51:01"}, {si:"1",dialed_no:"9184sd",connect_time:"2015-08-25 18:51:01"}]
and so on...
Note: I am getting the above input string from a API URL and not from a csv file or something.
Can you try this code and see if it works as you want?
//we split the single lines
$lines = explode("\n", $string);
$linesArray = array();
//we split each line in a set of elements
foreach($lines as $line){
$linesArray[] = explode(";",$line);
}
//we use the first line of data as an array of headers
$headers = $linesArray[0];
//and remove it
unset($linesArray[0]);
$jsonArray = [];
foreach($linesArray as $l=>$ln){
foreach($ln as $k=>$part){
//we re-build an array with the right headers
$jsonArray[$l][$headers[$k]] = $part;
}
}
print json_encode($jsonArray);
What you described as the output you want is not JSON.
Don't invent your own routines when PHP already has perfectly good ones (e.g. for JSON encoding and CSV parsing).
Assuming that the data is starting in a file....
$data=array();
$y=0;
$header=fgtetcsv($file_handle, 0, ';');
while (!feof($file_handle)) {
$row=fgtetcsv($file_handle, 0, ';');
foreach ($row as $x=>$value) {
$data[$y][$header[$x]]=$value;
}
$y++;
}
print json_encode($data);
Of course this will need some tweaks to handle error conditions and possibly for datasets larger than the working memory of php.
<?php
$records = array_map(
function($e) { // 2: to each line/record apply this functions
return str_getcsv($e, ';'); // 3: split the line/record into fields
},
explode( "\n", data() ) // 1: split data into "lines"/records
);
// 4: now $records is an array of records, each being an array of fields
$fields = array_shift($records); // 5: first record contains the field names, remove from array and assign to $fields
$records = array_map(
function($e) use ($fields) { // 7: this function has access to $fields, i.e. the names of the fields
return array_combine($fields, $e); // 8: see http://docs.php.net/array_combine
},
$records // 6: apply the function above to each element, i.e. record, in $records
);
echo json_encode($records);
function data() {
return <<< eot
si;dialed_no;connect_time;duration;region;call_cost
0;918592877727;2015-08-25 18:51:01;21;India(91);0.029
1;918907777727;2015-08-25 19:04:08;220;India(91);0.232
2;918907777727;2015-08-25 19:09:50;40;India(91);0.058
3;918907777727;2015-08-25 19:10:46;69;India(91);0.087
4;919048232151;2015-08-26 13:30:24;19;India(91);0.029
5;919895842822;2015-08-26 14:23:35;423;India(91);0.435
eot;
}
While I appreciate that VolkerK is using a lot of the right functions, I find that functional syntax creates too much bloat and makes the code harder to read. Furthermore, this task can be accomplished in a single loop and therefore it should be.
Code: (Demo)
$input = <<<SSV
si;dialed_no;connect_time;duration;region;call_cost
0;918592877727;2015-08-25 18:51:01;21;India(91);0.029
1;918907777727;2015-08-25 19:04:08;220;India(91);0.232
2;918907777727;2015-08-25 19:09:50;40;India(91);0.058
3;918907777727;2015-08-25 19:10:46;69;India(91);0.087
4;919048232151;2015-08-26 13:30:24;19;India(91);0.029
5;919895842822;2015-08-26 14:23:35;423;India(91);0.435
SSV;
$lines = explode(PHP_EOL, $input);
$header = str_getcsv(array_shift($lines), ';');
foreach ($lines as $line) {
$result[] = array_combine($header, str_getcsv($line, ';'));
}
echo json_encode($result, JSON_PRETTY_PRINT);
A note to the OP, you must not manually craft a json string. Always rely on the accuracy of json_encode() -- it won't fail you.

Retrieve word from string

I have this code:
$getClass = $params->get('pageclass_sfx');
var_dump($getClass); die();
The code above returns this:
string(24) "sl-articulo sl-categoria"
How can I retrieve the specific word I want without mattering its position?
Ive seen people use arrays for this but that would depend on the position (I think) that you enter these strings and these positions may vary.
For example:
$myvalue = $params->get('pageclass_sfx');
$arr = explode(' ',trim($myvalue));
echo $arr[0];
$arr[0] would return: sl-articulo
$arr[1] would return: sl-categoria
Thanks.
You can use substr for that in combination with strpos:
http://nl1.php.net/substr
http://nl1.php.net/strpos
$word = 'sl-categoria';
$page_class_sfx = $params->get('page_class_sfx');
if (false !== ($pos = strpos($page_class_sfx, $word))) {
// stupid because you already have the word... But this is what you request if I understand correctly
echo 'found: ' . substr($page_class_sfx, $pos, strlen($word));
}
Not sure if you want to get a word from the string if you already know the word... You want to know if it's there? false !== strpos($page_class_sfx, $word) would be enough.
If you know exactly what strings you're looking for, then stripos() should be sufficient (or strpos() if you need case-sensitivity). For example:
$myvalue = $params->get('pageclass_sfx');
$pos = stripos($myvalue, "sl-articulo");
if ($pos === FALSE) {
// string "sl-articulo" was not found
} else {
// string "sl-articulo" was found at character position $pos
}
If you need to check if some word are in string you may use preg_match function.
if (preg_match('/some-word/', 'many some-words')) {
echo 'some-word';
}
But this solution can be used for a small list of needed words.
For other cases i suggest you to use some of this.
$myvalue = $params->get('pageclass_sfx');
$arr = explode(' ',trim($myvalue));
$result = array();
foreach($arr as $key=> $value) {
// This will calculates all data in string.
if (!isset($result[$value])) {
$result[$value] = array(); // or 0 if you don`t need to use positions
}
$result[$value][] = $key; // For all positions
// $result[$value] ++; // For count of this word in string
}
// You can just test some words like follow:
if (isset($result['sl-categoria'])) {
var_dump($result['sl-categoria']);
}

CSV encoding php

Here's the problem i need to post a .csv file from one server to another.
I do this by reading the contents of the .csv file and sending that with curl as post data.
This is working without problems.
But then when i try to parse the data and store it in a table in the database the trouble begins.
I have all the variables in a array, if i print this array it displays correctly.
But if i echo a value from that array i get all kinds of weird characters.
My best guess is it has something to do with the encoding of the csv file but i wouldnt have a clue how to fix that.
here's the function i use to parse the csv data:
public function parseCsv($data)
{
$quote = '"';
$newline = "\n";
$seperator = ';';
$dbQuote = $quote . $quote;
// Clean up file
$data = trim($data);
$data = str_replace("\r\n", $newline, $data);
$data = str_replace($dbQuote,'"', $data);
$data = str_replace(',",', ',,', $data);
$data .= $seperator;
$inquotes = false;
$startPoint = $row = $cellNo = 0;
for($i=0; $i<strlen($data); $i++) {
$char = $data[$i];
if ($char == $quote) {
if ($inquotes) $inquotes = false;
else $inquotes = true;
}
if (($char == $seperator or $char == $newline) and !$inquotes) {
$cell = substr($data,$startPoint,$i-$startPoint);
$cell = str_replace($quote,'',$cell);
$cell = str_replace('"',$quote,$cell);
$result[$row][$this->csvMap[$cellNo]] = $this->_parseValue($cellNo, $cell);
++$cellNo;
$startPoint = $i + 1;
if ($char == $newline) {
$cellNo = 0;
++$row;
}
}
}
return $result;
}
any help is appreciated!
EDIT:
Ok so after some more trial and error i found out its just the very first value of the first row that has some extra characters. If i echo that value everything i output after that gets messed up.
So i tried to change the encoding now if i echo the value its all good but i have a new problem, its a string but i need a int:
echo $val; //output: 7655 but messes up everything outputted after it
$val = mb_convert_encoding($val, "UTF-8");
echo $val // output: 7655
echo intval($val) //output: 0
EDIT:
expected output:
7655Array ( [kenmerk] => ÿþ7655 [status] => 205 [status_date] => 1991-12-30 [dob] => 1936-09-04 ) succes
messed up output
7655牁慲੹ਨ††歛湥敭歲⁝㸽@㟾㘀㔀㔀਀††獛慴畴嵳㴠‾㈀㤀㔀਀††獛慴畴彳慤整⁝㸽 201ⴱ㄀㈀ⴀ30 †嬠潤嵢㴠‾㄀㤀㘀㘀-08〭㐀਀਩畳捣獥
i first echo the element 'kenmerk' after that i print the array
as you can see in the array the element 'kenmerk' has some extra charcters..
converting the data to utf-8 like so:
$data = mb_convert_encoding($data, "UTF-8");
eliminates the problem with messed up output and removes the 'ÿþ' (incorrectly-interpreted BOM?) but i still cant convert the values to a int
EDIT:
ok i sort of found a solution..
but as i have no idea why it works i'd appreciate any info
var_dump((int) $val); // output: 0
var_dump((int) strip_tags($val); // output: 7655
You need to remove ÿþ from 7655. intval() and int ($val = (int)$val;) will always output 0 when the first character is not a number. Ex. 765ÿþ5 will return 765, etc.
Regarding your first problem, I would also recommend you to read this answer. PHP messing with HTML Charset Encoding
I hope that it will give you more clarity about what you struggle with.
I will also build you striping process more stable, so it ex. match 7655 instead of ÿþ7655.

Check if string is serialized in PHP

I am in the middle of building a cache layer for the Redis DB to my application and I have come to the point where's it's about to take care of arrays.
I wonder if there's any good (high performance!) way of controlling an string to be serialized or not with PHP?
Thanks a lot!
$array = #unserialize($string);
if ($array === false && $string !== 'b:0;') {
// woops, that didn't appear to be anything serialized
}
The $string !== 'b:0;' checks to see if the serialized string may have been the value false. If this check is important to you you may want to trim the serialized string or otherwise preprocess it to make sure this works.
For those looking for alternative, this code worked for me as helper function for Laravel framework and then you can call it anywhere.
if(!function_exists('isSerialized')){
function isSerialized($string){
$tempString = '';
$array = #unserialize($string);
if(is_array($array)){
foreach ($array as $k=>$i){
//do something with data
$tempString .= $k . $i['something'];
}
} else {
$tempString = $string;
}
return $itemString;
}
}

Categories