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
$string = 'mystring';
$length = strlen($string);
for ($i = $length; $i > 0; $i--){
echo $string[$i-1];
}
OUTPUT: gnirtsym
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
We can do String Reverse with help of following menthods
$string = "Hello world!";
1st way to do:
echo strrev($string);
2nd way to do:
$stringSplit = str_split($string);
for ($i = $len-1; $i >=0;$i--)
{
echo $stringSplit[$i];
}
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
There is a function for this strrev
You can use it:
echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));