-
-
Save chang180/6887fc3448f274947bbeef73aa0111a1 to your computer and use it in GitHub Desktop.
中文字串反轉
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$str = '字串反轉'; | |
function rev($str, $encoding = 'utf-8'){ | |
$len = mb_strlen($str); | |
$result = ''; | |
for ($i = $len-1; $i>=0; $i--){ | |
$result.= mb_substr($str,$i,1,$encoding); | |
} | |
return $result; | |
} | |
echo rev($str); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function str_rev_gb($str){ | |
//判断输入的是不是utf8类型的字符,否则退出 | |
if(!is_string($str)||!mb_check_encoding($str,'UTF-8')){ | |
exit("输入类型不是UTF8类型的字符串"); | |
} | |
$array=array(); | |
//将字符串存入数组 | |
$l=mb_strlen($str,'UTF-8'); //返回具有 encoding 编码的字符串 str 包含的字符数.多字节的字符被计为 1。 | |
for($i=0;$i<$l;$i++){ | |
$array[]=mb_substr($str,$i,1,'UTF-8');//从索引为0处开始,每次截取一个字符,放入数组 | |
} | |
//反转字符串 | |
krsort($array); | |
//按照键名逆序排序 | |
//拼接字符串 | |
$string=implode($array); | |
//把数组元素组合成字符串 | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment