问题
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
回答1:
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];
}
回答2:
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
回答3:
$string = 'mystring';
$length = strlen($string);
for ($i = $length; $i > 0; $i--){
echo $string[$i-1];
}
OUTPUT: gnirtsym
回答4:
You must get $len-1
because string starts from 0
to $len-1
回答5:
There is a function for this strrev
回答6:
echo strrev("This is a reversed string!");
回答7:
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
回答8:
<?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
回答9:
for ($i = $len-1; $i >=0;$i--)
{
echo $stringExp[$i];
}
Since the index starts at 0
回答10:
Change your for loop to
for ($i = $len-1; $i >=0;$i--)
{
echo $stringExp[$i];
}
回答11:
<?php
echo strrev("PHP TUTORS");
?>
OUTPUT OF THE ABOVE SCRIPT
SROTUT PHP
Reference Source Code
回答12:
You can use it:
echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));
回答13:
public function stringReverse($string="Jai mata di")
{
$length = strlen($string) - 1;
$i = 0;
while ($i < $length + 1) {
echo $string[$length - $i];
$i++;
}
}
回答14:
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]; }
回答15:
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);
}
}
回答16:
<?php
/* Reverse a string with php */
echo strrev("Hello World!");
?>
Reverse a string php
回答17:
Find the length of string and through loop iterate it from length-1 to 0 then concatenate in temp variable.enter image description here
来源:https://stackoverflow.com/questions/11100634/reverse-a-string-with-php