PHP uses strrev to flip Chinese garbled characters,

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.