字符串:$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)."\n";
- echo "</pre>";
// 使用 for 循环方式, strlen($substr)
- $newStrThree = '';//初始化一个新的字符串
- $arrCountThree = strlen($str);
- for ($i = $arrCountThree; $i>=0;$i--) {
- @$newStrThree.=$str[$i];
- }
- echo "<pre>";
- print_r($newStrThree)."\n";
- echo "</pre>";
来源: http://www.bubuko.com/infodetail-1954397.html