Reverse a string with php

后端 未结 16 1024
南笙
南笙 2020-12-03 23:42

I am trying to find a way to reverse a string, I\'ve seen alternatives but i wanted to to it this way thinking outside the box and not using anyone else\'s code as an altern

相关标签:
16条回答
  • 2020-12-04 00:30

    Change your for loop to

    for ($i = $len-1; $i >=0;$i--)
    {
    echo $stringExp[$i];
    }
    
    0 讨论(0)
  • 2020-12-04 00:31
    echo strrev("This is a reversed string!"); 
    
    0 讨论(0)
  • 2020-12-04 00:32

    This will work

    class StringUtils {
    
        public function stringReverse($string){
            $arr1 = str_split($string);
            $arr2 = array();
            for($i = count($arr1); $i >= 0; $i--){
                $arr2[count($arr1) - $i] = $arr1[$i];
            }
            return implode("", $arr2);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 00:35
    public function stringReverse($string="Jai mata di")
    {
        $length = strlen($string) - 1;
        $i = 0;
        while ($i < $length + 1) {
            echo $string[$length - $i];
            $i++;
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题