|
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 )."
";
Echo $ str2. "->". str_rev_gb ($ str2 )."
";
Echo $ str3. "->". str_rev_gb ($ str3 )."
";
Echo $ str4. "->". str_rev_gb ($ str4 )."
";
|