PHP code for string flip (including Chinese characters,

Source: Internet
Author: User

PHP code for string flip (including Chinese characters,

Contain Chinese character strings garbled

PHP interview questions are short and common, but are more prone to errors. The questions are as follows:

How to Implement string flip?

First, of course, it's the strrev function. Isn't it really troublesome to put such an easy question into the interview? However, after reading the online answers, I found myself wrong ~~~

The strrev function is easy to use in English and can be used to flip strings directly, but what about Chinese? It must be garbled. There are many such problems, such as strstr and substr. Fortunately, PHP provides functions of the mb _ class for mutual conversion between different encodings and languages. Below is the PHP string flip function I wrote (mb _ class function needs to enable an mb_string implementation ).

Solution

Code:

<? Phpheader ("content-type: text/html; charset = UTF-8");/** strrev () function reverses the string. The description of the strrev (string) parameter is required. Specifies the string to be reversed. * // ** This function is used to reverse the string mb_strlen () to obtain the length of the character mb_substr () to obtain a single element of the character krsort () to sort the array in reverse order according to the key value, sort the joined Array in descending order by key name. Array ([8] => country [7] => medium [6] => h [5] => s [4] => I [3] => l [2] => g [1] => n [0] => E) in Chinese, hsilgnE (ksort () sorts the associated array in ascending order by key name) implode () Concatenates the array into a string explode () use a string to separate the string */function str_rev_gb ($ str) {// identify whether the input is a UTF-8 character; otherwise, exit if (! Is_string ($ str) |! Mb_check_encoding ($ str, 'utf-8') {exit ("the input type is not a UTF-8 string");} $ array = array (); // Save the string to the array $ l = mb_strlen ($ str, 'utf-8'); // when mb_strlen is calculated, the selected inner code is UTF8, A Chinese character is regarded as 1 to calculate for ($ I = 0; $ I <$ l; $ I ++) {$ array [] = mb_substr ($ str, $ I, 1, 'utf-8');} // reverse the string krsort ($ array); // concatenate the string $ string = implode ($ array ); return $ string ;}$ str1 = "Englist"; $ str2 = "English China"; $ str3 = "Eng 中llish"; $ str4 = "People's Republic of China "; echo 'directly Use PHP's built-in strrev function to reverse :'. '<br/>'; echo $ str1. "-> ". strrev ($ str1 ). "<br>"; echo $ str2. "-> ". strrev ($ str2 ). "<br>"; echo $ str3. "-> ". strrev ($ str3 ). "<br>"; echo $ str4. "-> ". strrev ($ str4 ). "<br>"; echo '<br/>'; echo $ str1. "-> ". str_rev_gb ($ str1 ). "<br>"; echo $ str2. "-> ". str_rev_gb ($ str2 ). "<br>"; echo $ str3. "-> ". str_rev_gb ($ str3 ). "<br>"; echo $ str4. "-> ". str_rev_gb ($ str4 ). "<br> ";

Running result:

Use the strrev function in PHP to reverse the query:
Englist-> tsilgnE
English China-> hhsilgne
Eng China lish> �� hsil �� gnE
People's Republic of China-> chinanetcenter

Englist-> tsilgnE
English China> China hsilgnE
Eng-lish-> gnE in hsil
People's Republic of China> China and Central China

It should be noted that mb_strlen is not a PHP core function. before using it, make sure that. the "php_mbstring.dll" line is loaded in ini to ensure that the "extension = php_mbstring.dll" line exists and is not commented out. Otherwise, the number of undefined functions may occur.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.