Classification: Practical knowledge
Reading (5590).
Comments (0)
In the use of PHP’s unserialize function, the following error occurs when a string or data is fetched from a database in Chinese
Notice: unserialize(): Error at offset xx of xxx bytes in C:\toolmao\php\index.php on line 21
Speaking of Chinese, we must first think of coding, so let’s see how the serialize function handles Chinese with different file codes, using the same piece of code and testing the file with different codes
echo serialize(array('name'=>'Tools','url'=>'http://www.toolmao.com/'));
Looking at the UTF8 effect first, we find that the Length of the Chinese “tool cat” is 9
When the file code is GB2312, the length of Chinese “tool cat” is 6
Therefore, when we take out the database encoding GBK and other Chinese encoding, and then use transcoding function to convert to UTF8, and then use unserialize function, this problem will occur because the length of Chinese in different encodings is different. One might say that SET NAMES is not acceptable when the database is pulled out, but some databases (such as Access and SQLite) do not support this. So when the code does not match, you must take out the data transcoding. So how do you do that?Here’s the solution:
Above, we have already known the cause of Error at offset in unserialize. Therefore, it is easy to solve this problem. Just replace the Error length with the regular function.
unserialize(preg_replace('!s:(\d+):"(.*?)";!se', '"s:".strlen("$2").":\"$2\";"', $string)) From:http://www.phpddt.com/php/unserialize-error-at-offset.html
However, due to the PHP problem, the/E mode has been compromised, so this usage has been removed since PHP5.5, so don’t worry if you are using PHP5.5 +, here is another solution
preg_replace_callback('#s:(\d+):"(.*?)";#s',function($match){return 's:'.strlen($match[2]).':"'.$match[2].'";';},$data);
The above is my actual development encountered in the problem, combined with the solution of the net and their own actual situation finally write the code, I hope to help you!
Read More:
- php Error: mail() Failed to connect to mailserver at “localhost” port 25
- 404 solution for WordPress Chinese tag
- Laravel-admin php artisan admin:install error reporting problem solution
- PHP encryption 3DES error Call to undefined function: mcrypt_module_open() solution
- PHP use __Sleep() and __wakeup() method serializes the object
- [PHP]json_encode Chinese JSON_UNESCAPED_UNICODE returns null in php5.3
- [Solved] Artisan error: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
- PHP: How to parse MHT file into HTML
- Mac: make: *** [Zend/zend_language_parser.lo] Error 1
- [Solved] PHP post Datas json_decode Error: 4 JSON_ERROR_SYNTAX
- [Solved] PDOException::(“SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long;
- [Solved] Warning: Unknown: Failed to write session data using user defined save handler. (session.save_path:
- Laravel5.5 Error: SQLSTATE[42000] Syntax error or access violation 1071 Specified key was too long; max k
- How to solve Magento 500 internal server errors
- TP5 fuzzy Chinese characters error [How to Solve]
- [Solved] Laravel Upload Files Verificate Error: The file failed to upload.
- PHP Error Object of class mysqli could not be converted to string in
- [PHP] __autoload function will be called when class_exists does not exist
- [Solved] WordPress Upgrade PHP 5.6 to 7.x Fatal error: Uncaught Error: Call to undefined function mysql_connect()
- [PHP] Array to string conversion error when sending data in post