|
<? Php
Header ("content-type: text/html; charset = UTF-8 ");
/**
This function is used to reverse a Chinese string.
Mb_strlen () gets the length of Characters
Mb_substr () gets a single element of a character.
Krsort () sorts arrays by key value in reverse order
Implode () concatenates an array into a string
Explode () uses strings to separate strings
*/
Function str_rev_gb ($ str ){
// Determine 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 an array
$ L = mb_strlen ($ str, 'utf-8 ');
For ($ I = 0; $ I <$ l; $ I ++ ){
$ Array [] = mb_substr ($ str, $ I, 1, 'utf-8 ');
}
// Reverse the string
Krsort ($ array );
// Concatenate a string
$ String = implode ($ array );
Return $ string;
}
$ Str1 = "Englist ";
$ Str2 = "English China ";
$ Str3 = "Eng central lish country ";
$ Str4 = "People's Republic of China ";
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> ";
|