How to unserialize this kind of String in Dart?
I've made a translation to Dart of Nicolas Chambrier's javascript implementation to unserialize PHP strings.
Given a serialized string in php, translates and returns a Map<String, dynamic> object.
Here is a direct link to the class php_unserialize.dart, and you can also check how to use it here.
Usage:
String _sample = 'O:8:"stdClass":5:{s:4:"name";s:7:"Rodrigo";s:4:"last";s:7:"Cardozo";s:3:"age";i:35;s:9:"developer";b:1;s:6:"height";d:73.5;}';
var object = Php.unserialize(_sample);
print(object["name"]);
What string format is this? How do I decode
This is a serialised array.
You can use the following to decode it and access it through PHP.
<?php
//Decode the string
$decode = unserialize('a:2:{i:0;i:141;i:1;i:462;}');
//Decode will give you this
Array (
[0] => 141
[1] => 462
)
//Accessible by
echo $decode[0]; //will give you 141
echo $decode[0]; //will give you 462
?>
How to find out if a string is a serialized object/array or just a string?
Well, you can tell by looking at the format. When you serialize an array, you get a string that looks like a:1:{i:0;s:3:"foo"}
And if you serialize an object, you get: o:7:"myclass":1:{s:3:"foo";s:3:"bar";}
.
So if you want to test rudimentary, you can do these two regexes:
^a:\d+:{.*?}$
And
^o:\d+:"[a-z0-9_]+":\d+:{.*?}$
for arrays and objects respectively.
Note that this just checks for the generic form. To tell if it's a valid serialized string, you need to run it through unserialize()
and test the return is_array($result)
and is_object($result)
...
Uable to unserialize a serialized string
This is because wrong serialize data is getting created from it.Even if we try to create JSON data, it is creating json data with wrong syntax.
If we will try bellow codes, PDO will not allow it.
$equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? str_replace("'","", $_POST['equipment']['tables']) : '';
$equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? addslashes($_POST['equipment']['tables']) : '';
$equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? str_replace("'","\'", $_POST['equipment']['tables']) : '';
results server error 500 in this case.
and
$equipment['tables'] = isset( $_POST['equipment']['tables'] ) ? addcslashes($_POST['equipment']['tables']) : '';
will results in NULL.
We can can use either of 2 and 3 and change our PDO like this, that will solve the problem.
unserialize function is not returning returning string in same format
Your string is serialized twice,
s:33:"a:2:{i:0;s:2:"80";i:1;s:3:"112";}";
is a string (with 33 chars) encoded, so unserialize()
this gives you another string which is
a:2:{i:0;s:2:"80";i:1;s:3:"112";}
you also need to unserialize()
that...
$industries = 's:33:"a:2:{i:0;s:2:"80";i:1;s:3:"112";}";';
$industries = unserialize($industries);
print_r(unserialize($industries));
gives...
Array
(
[0] => 80
[1] => 112
)
PHP: Unserialize string
PHP documentation for unserialize
PHP code demo
<?php
$result=unserialize('a:3:{i:0;s:28:"Logo_final_SatuPintu (1).png";i:1;s:24:"Logo_final_SatuPintu.png";i:2;s:44:"WhatsApp Image 2017-03-24 at 7.04.01 PM.jpeg";}');
print_r($result);
Output:
Array
(
[0] => Logo_final_SatuPintu (1).png
[1] => Logo_final_SatuPintu.png
[2] => WhatsApp Image 2017-03-24 at 7.04.01 PM.jpeg
)
How to unserialize a string to array
This is a serialized
string you should unserialize
it using this function unserialize.
<?php
ini_set('display_errors', 1);
$string='a:2:{i:0;s:2:"25";i:1;s:2:"26";}';
print_r(unserialize($string));
Output:
Array
(
[0] => 25
[1] => 26
)
Related Topics
Get First Key in a (Possibly) Associative Array
Using Jquery Ajax to Retrieve Data from MySQL
PHP String Replace Match Whole Word
Change Cart Item Prices in Woocommerce 3
Convert Dot Syntax Like "This.That.Other" to Multi-Dimensional Array in PHP
Laravel: Error [Pdoexception]: Could Not Find Driver in Postgresql
A Numeric String as Array Key in PHP
How to Truncate a String to the First 20 Words in PHP
How to Retract a Salted Password from the Database and Auth User
How to Push Both Value and Key into PHP Array
Why Would Json_Encode Return an Empty String
Storing Files in Database VS File System
MySQLi Bind_Param() Expected to Be a Reference, Value Given
Does PHP Have Short-Circuit Evaluation
PHP Warning: Call-Time Pass-By-Reference Has Been Deprecated