4

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 alternative, the code below reverses the string but I keep getting this error:

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

25 being the length of the string which is being deincremented.

//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];
}

thanks in advance

Steven Mcsorley
  • 81
  • 1
  • 1
  • 7
  • Your string index starts at 0, so the 25th character's index is 24. Change your code so that $i goes from 24 to 0. As many posters have answered below, you should also look for a function to do what you need in the PHP function libraries. – Gustav Bertram Jun 19 '12 at 12:16
  • Thanks I stumbled on the 0 being counted as One, and yes there are libraries to do this but I wanted to try and do it differently without them ;-) – Steven Mcsorley Jun 19 '12 at 13:20
  • I added an answer on your problem here, http://stackoverflow.com/a/30722030/137196. Feel free to adjust as needed. Thanks – Louie Miranda Jun 09 '15 at 02:29

16 Answers16

8

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
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 1
    Be careful when using `strrev` on a utf8 string! [See this answer](http://stackoverflow.com/questions/17496493/strrev-dosent-support-utf-8/17496494#17496494) – mixable Sep 20 '15 at 13:37
4

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];
}
Kevin R
  • 8,230
  • 4
  • 41
  • 46
oezi
  • 51,017
  • 10
  • 98
  • 115
  • Thanks that's where I was going wrong forgot that 0 it counted so when deincrimenting the length it was looking for 25 which wasn't there, and yes I know strrev is available but i was trying to do it without relying on pre-built functions. Youve saved me from a headache :-) – Steven Mcsorley Jun 19 '12 at 12:37
4
$string = 'mystring';
$length = strlen($string);

for ($i = $length; $i > 0; $i--){
echo $string[$i-1];
}

OUTPUT: gnirtsym
Dinesh Chandra
  • 662
  • 3
  • 12
  • 23
2

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

Laser
  • 6,652
  • 8
  • 54
  • 85
1

There is a function for this strrev

Stanislav
  • 98
  • 2
1
echo strrev("This is a reversed string!"); 
Raab
  • 34,778
  • 4
  • 50
  • 65
1

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
Rinzler
  • 2,139
  • 1
  • 27
  • 44
  • Thanks Yes strrev is the easy way but I was trying to work it out methodically ie(the long way) its for an interview I have today. – Steven Mcsorley Jun 19 '12 at 12:40
1
<?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
Vishnu Sharma
  • 1,357
  • 12
  • 11
0
for ($i = $len-1; $i >=0;$i--)
{
  echo $stringExp[$i];
}

Since the index starts at 0

Boris Delormas
  • 2,509
  • 1
  • 19
  • 27
0

Change your for loop to

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

OUTPUT OF THE ABOVE SCRIPT

SROTUT PHP

Reference Source Code

Hassan Amir Khan
  • 645
  • 8
  • 13
0

You can use it:

echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
Sudhir
  • 835
  • 11
  • 31
0
public function stringReverse($string="Jai mata di")
{
    $length = strlen($string) - 1;
    $i = 0;
    while ($i < $length + 1) {
        echo $string[$length - $i];
        $i++;

    }
}
Gopal Sharma
  • 755
  • 1
  • 12
  • 25
0

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];
                }
    
Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
0

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
<?php
/* Reverse a string with php */
echo strrev("Hello World!"); 
?>

Reverse a string php

Azouz Mohamadi
  • 141
  • 1
  • 2