Reverse a string with php

后端 未结 16 1023
南笙
南笙 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:21
    $string = 'mystring';
    $length = strlen($string);
    
    for ($i = $length; $i > 0; $i--){
    echo $string[$i-1];
    }
    
    OUTPUT: gnirtsym
    
    0 讨论(0)
  • 2020-12-04 00:21

    php is quite complete in term of string function you just need to pass the string . thats why php is easy :)

    use strrev php function http://bg2.php.net/manual/en/function.strrev.php

    <?php
      echo strrev("This is a reversed string");
     ?>
    
    // Output: gnirts desrever a si sihT
    
    0 讨论(0)
  • 2020-12-04 00:21

    We can do String Reverse with help of following menthods

        $string = "Hello world!";
    
    1. 1st way to do:

      echo strrev($string);
      
    2. 2nd way to do:

       $stringSplit = str_split($string);
                  for ($i = $len-1; $i >=0;$i--)
                  {
                  echo $stringSplit[$i];
                  }
      
    0 讨论(0)
  • 2020-12-04 00:23

    You're trying much too hard, always consult the manual and/or a search engine to check if there are native functions to do what you want before you end up "reinventing the wheel":

    strrev — Reverse a string

    http://php.net/manual/en/function.strrev.php

    $string = "This is a reversed string";
    echo strrev($string);
    // Output: gnirts desrever a si sihT
    
    0 讨论(0)
  • 2020-12-04 00:26

    There is a function for this strrev

    0 讨论(0)
  • 2020-12-04 00:28

    You can use it:

    echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));
    
    0 讨论(0)
提交回复
热议问题