php 中字符串翻转的方法实例代码

2019-12-21 09:42:06 1053
'

方法一(直接使用php自带函数strrev($str))

print_r(strrev($str));

使用for循环方式,str_split($str)


1
2
3
4
5
6
7
8
9
10
$newarrone = [];//初始化一个新的数组
 $newstrone = '';//初始化一个新的字符串
 $newarrone = str_split($str);
 $arrcount = count($newarrone);
 for ($i=0; $i < $arrcount; $i++) {
 $newstrone.=$newarrone[$i];
 }
 echo "<pre>";
 print_r($newstrone);
 echo "</pre>";


使用for循环方式,strlen($substr)


1
2
3
4
5
6
7
8
$newstrtwo = '';//初始化一个新的字符串
 $arrcounttwo = strlen($str);
 for ($i=1; $i <= $arrcounttwo; $i++) {
 $newstrtwo.=substr($str, -$i, 1);
 }
 echo "<pre>";
 print_r($newstrtwo)."\n";
 echo "</pre>";


使用for循环方式,strlen($substr)


1
2
3
4
5
6
7
8
$newstrthree = '';//初始化一个新的字符串
$arrcountthree = strlen($str);
for ($i = $arrcountthree; $i>=0;$i--) {
 @$newstrthree.=$str[$i];
}
echo "<pre>";
print_r($newstrthree)."\n";
echo "</pre>"; 

以上就是php 中字符串翻转的方法实例代码的详细内容,更多请关注php中文网其它相关文章!

'