Invert string with php

I am trying to find a way to change the line, I saw alternatives, but I wanted it to think outside the box this way and not use any other code as an alternative, the code below changes the line, but I keep getting this error:

Note: Undefined offset: 25 in C: \ wamp \ www \ test \ index.php on line 15

25 - the length of the string, which is de-incremented.

//error_reporting(NULL); $string = trim("This is a reversed string"); //find length of string including whitespace $len =strlen($string); //slipt sting into an array $stringExp = str_split($string); //deincriment string and echo out in reverse for ($i = $len; $i >=0;$i--) { echo $stringExp[$i]; } 

early

+4
source share
15 answers

As others have said, there strrev() for this.

If you want to build it yourself (for training?): Your problem is that you start with your index one too high - a line of length 25 is indexed from 0 to 24, so your loop should look like this:

 for ($i = $len - 1; $i >=0;$i--) { echo $stringExp[$i]; } 
+3
source

You try too much, always consult the manual and / or the search engine to check if you have your own functions to do what you want before you finish β€œreinvent the wheel”:

strrev - reverse string

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

 $string = "This is a reversed string"; echo strrev($string); // Output: gnirts desrever a si sihT 
+8
source
 $string = 'mystring'; $length = strlen($string); for ($i = $length; $i > 0; $i--){ echo $string[$i-1]; } OUTPUT: gnirtsym 
+3
source

You should get $len-1 because the line starts from 0 to $len-1

+1
source

There is strrev for this function

+1
source
 echo strrev("This is a reversed string!"); 
+1
source

php is pretty complete in terms of a string function, you just need to pass the string. that's 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 
+1
source
 <?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; 

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

+1
source
 for ($i = $len-1; $i >=0;$i--) { echo $stringExp[$i]; } 

Since the index starts at 0

0
source

Change the for loop to

 for ($i = $len-1; $i >=0;$i--) { echo $stringExp[$i]; } 
0
source
 <?php echo strrev("PHP TUTORS"); ?> 

EXIT FROM ABOVE SCRIPT

 SROTUT PHP 

Link source code

0
source

You can use it:

 echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World"))); 
0
source
 public function stringReverse($string="Jai mata di") { $length = strlen($string) - 1; $i = 0; while ($i < $length + 1) { echo $string[$length - $i]; $i++; } } 
0
source

We can do String Reverse with the following menthods

  $string = "Hello world!"; 
  • The first way to do:

     echo strrev($string); 
  • The second way:

      $stringSplit = str_split($string); for ($i = $len-1; $i >=0;$i--) { echo $stringSplit[$i]; } 
0
source

It 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
source

All Articles