In php, unserialize returns false. In php, unserialize returns false. unserializefalse this article describes how to return false for unserialize in php. The specific method is as follows: php unserialize returns false solution, unserializefalse
This article describes how to return false for unserialize in php. The specific method is as follows:
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.
Let's take a look at the following program example:
<? 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 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:
// utf8 function 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); } // ascii function 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'
'; // Utf8 function 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); // false var_dump (mb_unserialize ($ str); // correct
The mb_unserialize method for filtering \ r can be deserialized successfully by processing single double quotes.
Use unserialize:
Bool (false)
Use mb_unserialize
Array (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 "}
I hope this article will help you learn PHP programming.
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.
Examples in this article describes how to return false for unserialize in php and share it with you for your reference. The specific method is as follows :...