<?php
header("Content-Type:text/html;charset=utf-8");
//方法一 使用内置函数
echo strrev('libraryRev');
//方法二 使用substr获取子字符串
function getRev($str,$encoding='utf-8'){
$result = '';
$len = mb_strlen($str,$encoding);
for($i=$len-1; $i>=0; $i--){
$result .= mb_substr($str,$i,1,$encoding);
}
return $result;
}
echo getRev('中文翻转');
//方法三 使用str_split转换为数组
function getRevByArray($str)
{
$result = '';
$tempArray = str_split($str);
$length = count($tempArray);
for($i = $length - 1; $i >= 0; $i--){
$result .= $tempArray[$i];
}
return $result;
}
echo getRevByArray('test');
?>
PHP实现字符串翻转
最新推荐文章于 2023-07-20 23:03:01 发布