自己写的php反转字符串函数

function reverse($str){
$str_reverse='';
for($i=strlen($str);$i>0;$i–){
$str_reverse=$str_reverse.substr($str,$i-1,1);
$str=substr($str,0,$i-1);
}
return $str_reverse;
}

没用递归,但是很恶心啊,比php内置的strrev慢20倍,郁闷……

3 thoughts on “自己写的php反转字符串函数

  1. coder4123

    function reverseStr($str = ”) {
    $count = strlen($str) – 1;
    if ($count) {
    $retStr = ”;
    while($count >= 0) {
    $retStr .= $str{$count–};
    }
    return $retStr;
    }
    }

    Reply
    1. coder4zzbo

      $str = ‘abcde’;
      $arr1 = str_split($str);
      $arr2 = array_reverse($arr1);
      echo implode(”,$arr2);

      Reply

Leave a Reply

Your email address will not be published.