Php unserialize returns false, unserializefalse
Php unserialize returns false
Php provides the serialize (serialization) and unserialize (deserialization) methods.
After serialize is used for serialization, you can use unserialize for deserialization to obtain the original data.
<? Php $ arr = array ('name' => 'fdipzone ', 'gender' => 'male'); $ str = serialize ($ arr ); // serialize echo 'serialize str :'. $ str. "\ r \ n"; $ content = unserialize ($ str); // deserialize echo "unserialize str: \ r \ n "; var_dump ($ content);?>
Output:
serialize str:a:2:{s:4:"name";s:8:"fdipzone";s:6:"gender";s:4:"male";}unserialize str:array(2) { ["name"]=> string(8) "fdipzone" ["gender"]=> string(4) "male"}
However, in the following example, deserialization will return false.
<? Php $ str = 'a: 9: {s: 4: "time"; I: 1405306402; s: 4: "name"; s: 6: "New morning"; s: 5: "url"; s: 1: "-"; s: 4: "word"; s: 1: "-"; s: 5: "rpage"; s: 29: "http://www.baidu.com/test.html"; s: 5: "cpage"; s: 1: "-"; s: 2: "ip"; s: 15: "117.151.180.150"; s: 7: "ip_city"; s: 31: "Beijing, China Mobile"; s: 4: "miao"; s: 1: "5 ";}'; var_dump (unserialize ($ str); // bool (false)?>
Check the serialized string and find the problem is in two places
S: 5: "url"
S: 29: "http://www.baidu.com/test.html"
The two parts should be
S: 3: "url"
S: 30: "http://www.baidu.com/test.html"
This problem occurs because the encoding during data serialization is inconsistent with the encoding during deserialization, for example, the database is latin1 and the UTF-8 character length is different.
In addition, there may be a single double quotation mark. The ascii character "\ 0" is parsed as '\ 0', and \ 0 in C is the string Terminator equal to chr (0 ), 2 characters after error parsing.
\ R also causes problems when calculating the length.
The solution is as follows:
// utf8function mb_unserialize($serial_str) { $serial_str= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str ); $serial_str= str_replace("\r", "", $serial_str); return unserialize($serial_str);}// asciifunction asc_unserialize($serial_str) { $serial_str = preg_replace('!s:(\d+):"(.*?)";!se', '"s:".strlen("$2").":\"$2\";"', $serial_str ); $serial_str= str_replace("\r", "", $serial_str); return unserialize($serial_str);}
Example:
Echo '<meta http-equiv = "content-type" content = "text/html; charset = UTF-8">'; // utf8function mb_unserialize ($ serial_str) {$ serial_str = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s :'. strlen ('$2 '). ': \ "$2 \";' ", $ serial_str); $ serial_str = str_replace (" \ r "," ", $ serial_str ); return unserialize ($ serial_str) ;}$ str = 'a: 9: {s: 4: "time"; I: 1405306402; s: 4: "name"; s: 6: "New morning"; s: 5: "url"; s: 1: "-"; s: 4: "word"; s: 1: "-"; s: 5: "rpage"; s: 29: "http://www.baidu.com/test.html"; s: 5: "cpage"; s: 1: "-"; s: 2: "ip"; s: 15: "117.151.180.150"; s: 7: "ip_city"; s: 31: "Beijing, China Mobile"; s: 4: "miao"; s: 1: "5";} '; var_dump (unserialize ($ str); // falsevar_dump (mb_unserialize ($ str); // correct
The mb_unserialize Method for filtering \ r can be deserialized successfully by processing single double quotes.
Use unserializebool (false) to use mb_unserializearray (9) {["time"] => int (1405306402) ["name"] => string (6) "XinChen" ["url"] => string (1) "-" ["word"] => string (1) "-" ["rpage"] => string (30) "http://www.baidu.com/test.html" ["cpage"] => string (1) "-" ["ip"] => string (15) "117.151.180.150" ["ip_city"] => string (31) "Beijing, China Mobile" ["miao"] => string (1) "5 "}
Php's unserialize () function returns false. How can this problem be solved,
If false is returned, it indicates that your data is faulty. Check whether the format of your stored data is correct.
In PHP, unserialize ($ str) returns FALSE, and $ str is printed and written into parentheses.
This is the encoding problem. the encoding of the database is inconsistent with that of the PHP file.
After you print the text and copy it and paste it, it is actually a code conversion.