Solve the problem of strrev function flipping Chinese garbled in PHP

Source: Internet
Author: User
Tags ord
When you flip Chinese with strrve in PHP, garbled behavior occurs.

For example:

Header ("content-type:text/html; Charset=utf-8 "), Echo strrev (" Handling garbled problems using the Strrev () function ")." <br> ";


The result of the operation is:

鮗 copy 䄚 綗 protium) (verrts 翽 䆐 焤

The solution is to rewrite a cnstrrev function yourself.

Header ("content-type:text/html; Charset=utf-8 "), $str = ' handling garbled problem using Strrev () function '; function Cnstrrev ($str) {    $len = strlen ($STR);    for ($i = 0; $i < $len; $i + +)    {        $char = $str {0};        if (ord ($char) > 127)//ord () function takes the ASCII code of the first character, if it is greater than 0xa0 (127) then the Chinese character        {            $i +=2;//utf-8 encoding, a Chinese character is three bytes            if ($i < $len)            {                $arr [] = substr ($str, 0, 3);//utf-8 encoding, a Chinese character occupies three bytes                $str = substr ($str, 3);            }        }        else        {            $arr [] = $char;            $str = substr ($str, 1);//Otherwise non-Chinese, takes one character        }    }    return Join (Array_reverse ($arr));//returns an array in reverse order of elements:}echo $ Str. ' + '. Cnstrrev ($STR);

The result of the operation is:

Handling garbled problems when using the Strrev () function + the number of hours of confusion in the question (Verrts)

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.