PHP uses strrev to flip Chinese garbled characters,
This example describes how to solve the Chinese garbled characters flipped by strrev in PHP. We will share this with you for your reference. The details are as follows:
Garbled characters may occur when I use strrve in PHP to flip Chinese Characters
For example:
Header ("Content-Type: text/html; charset = UTF-8"); echo strrev ("handling garbled characters when using the strrev () function"). "<br> ";
The running result is:
�� Too many threads have been written) (verrts ��)
The solution is to rewrite a cnstrrev function.
Header ("Content-Type: text/html; charset = UTF-8"); $ str = 'processing garbled characters when using the strrev () function '; function cnstrrev ($ str) {$ len = strlen ($ str); for ($ I = 0; $ I <$ len; $ I ++) {$ char = $ str {0 }; if (ord ($ char)> 127) // The ord () function obtains the ASCII code of the first character. if it is greater than 0xa0 (127) it is a Chinese character {$ I + = 2; // UTF-8 encoding, a Chinese character occupies three bytes if ($ I <$ len) {$ arr [] = substr ($ str, 0, 3); // in UTF-8 encoding, a Chinese character occupies three bytes. $ str = substr ($ str, 3) ;}} else {$ arr [] = $ char; $ str = substr ($ str, 1); // otherwise it is not Chinese, occupies one character} return join (array_reverse ($ arr); // returns an array in reverse element order:} echo $ str. '+ '. cnstrrev ($ str );
The running result is:
Handle garbled questions when using the strrev () function + the number of hours when the question code is messy)