Reverse a string with php

后端 未结 16 1022
南笙
南笙 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:12

    As others said, there's strrev() to do this.

    If you want to build it on your own (for learning?): your problem is that you're starting with your index one too high - a string of length 25 is indexed from 0 to 24, so your loop has to look like this:

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

    You must get $len-1 because string starts from 0 to $len-1

    0 讨论(0)
  • 2020-12-04 00:17
    <?php
    // Reversed string and Number
    //  For Example :
        $str = "hello world. This is john duvey";
        $number = 123456789;
        $newStr = strrev($str);
        $newBum = strrev($number);
    
        echo $newStr;
        echo "<br />";
        echo $newBum;
    

    OUTPUT : first : yevud nhoj si sihT .dlrow olleh second: 987654321

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

    Since the index starts at 0

    0 讨论(0)
  • 2020-12-04 00:18
    <?php
    echo strrev("PHP TUTORS");
    ?>
    

    OUTPUT OF THE ABOVE SCRIPT

    SROTUT PHP
    

    Reference Source Code

    0 讨论(0)
  • 2020-12-04 00:20
    <?php
    /* Reverse a string with php */
    echo strrev("Hello World!"); 
    ?>
    

    Reverse a string php

    0 讨论(0)
提交回复
热议问题