gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

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

来源:http://erdangjiade.com/topic/1389.html 你好,世界。 2017-09-25 22:42浏览(10)

本文主要介绍了实现php字符串翻转的方法,具有很好的参考价值,下面跟着小编一起来看下吧

字符串:$str = "abcdefg";

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

print_r(strrev($str));

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

$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)

$newStrTwo = '';//初始化一个新的字符串
 $arrCountTwo = strlen($str);
 for ($i=1; $i <= $arrCountTwo; $i++) {
 $newStrTwo.=substr($str, -$i, 1);
 }
 echo "<pre>";
 print_r($newStrTwo)."
";
 echo "</pre>";

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

$newStrThree = '';//初始化一个新的字符串
$arrCountThree = strlen($str);
for ($i = $arrCountThree; $i>=0;$i--) {
 @$newStrThree.=$str[$i];
}
echo "<pre>";
print_r($newStrThree)."
";
echo "</pre>"; 

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

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2