How to reverse a string?


The ideal solution to reverse a string.

Define and use a function like this:

function get_reverse_string($str_left_right)

{

   // we get the lenght of our string using strlen function

   $string_lenght = strlen($str_left_right);

   if($string_lenght == 1)

   {

      //we have nothing to reverse: we return the same string

      $str_right_left = $str_left_right;

      return $str_right_left;

   } else

   {

      $string_lenght--;

      $str_right_left = get_reverse_string(substr($str_left_right,1, $string_lenght)) . substr($str_left_right, 0, 1);

     return $str_right_left;

   }

}

 

Related articles