Skip to content

Instantly share code, notes, and snippets.

@chang180 chang180/rev
Last active Mar 26, 2020

Embed
What would you like to do?
中文字串反轉
$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);
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
You can’t perform that action at this time.