Steven Mcsorley
Steven Mcsorley

Reputation: 81

Reverse a string with php

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

Upvotes: 4

Views: 27492

Answers (12)

Volodymyr Shalashenko
Volodymyr Shalashenko

Reputation: 159

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);
    }
}

Upvotes: 0

oezi
oezi

Reputation: 51797

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

Upvotes: 4

Gopal Sharma
Gopal Sharma

Reputation: 825

public function stringReverse($string="Jai mata di")
{
    $length = strlen($string) - 1;
    $i = 0;
    while ($i < $length + 1) {
        echo $string[$length - $i];
        $i++;

    }
}

Upvotes: 0

Sudhir
Sudhir

Reputation: 837

You can use it:

echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));

Upvotes: 0

Dinesh Chandra
Dinesh Chandra

Reputation: 672

$string = 'mystring';
$length = strlen($string);

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

OUTPUT: gnirtsym

Upvotes: 4

Rinzler
Rinzler

Reputation: 2116

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 https://www.php.net/manual/en/function.strrev.php

<?php
  echo strrev("This is a reversed string");
 ?>

// Output: gnirts desrever a si sihT

Upvotes: 2

Rab
Rab

Reputation: 35572

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

Upvotes: 1

Stanislav
Stanislav

Reputation: 98

There is a function for this strrev

Upvotes: 1

Laser
Laser

Reputation: 6960

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

Upvotes: 2

user1432124
user1432124

Reputation:

Change your for loop to

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

Upvotes: 0

Boris Delormas
Boris Delormas

Reputation: 2549

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

Since the index starts at 0

Upvotes: 0

No Results Found
No Results Found

Reputation: 102745

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

Upvotes: 8

Related Questions