php 用php反转字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11100634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse a string with php
提问by Steven Mcsorley
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
注意:未定义偏移量:第 15 行 C:\wamp\www\test\index.php 中的 25
25 being the length of the string which is being deincremented.
25 是被递减的字符串的长度。
//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
提前致谢
采纳答案by oezi
As others said, there's strrev()to do this.
正如其他人所说,strrev()必须这样做。
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:
如果你想自己构建它(为了学习?):你的问题是你的索引太高了 - 长度为 25 的字符串从 0 到 24 索引,所以你的循环必须看起来像这样:
for ($i = $len - 1; $i >=0;$i--)
{
echo $stringExp[$i];
}
回答by Wesley Murch
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
strrev— 反转字符串
http://php.net/manual/en/function.strrev.php
http://php.net/manual/en/function.strrev.php
$string = "This is a reversed string";
echo strrev($string);
// Output: gnirts desrever a si sihT
回答by Dinesh Chandra
$string = 'mystring';
$length = strlen($string);
for ($i = $length; $i > 0; $i--){
echo $string[$i-1];
}
OUTPUT: gnirtsym
回答by Raab
echo strrev("This is a reversed string!");
回答by Rinzler
php is quite complete in term of string function you just need to pass the string . thats why php is easy :)
php 在字符串函数方面非常完整,您只需要传递字符串。这就是为什么 php 很容易:)
use strrevphp function http://bg2.php.net/manual/en/function.strrev.php
使用strrevphp 函数http://bg2.php.net/manual/en/function.strrev.php
<?php
echo strrev("This is a reversed string");
?>
// Output: gnirts desrever a si sihT
回答by Pepelac
You must get $len-1because string starts from 0to $len-1
你必须得到,$len-1因为字符串从0到$len-1
回答by Vishnu Sharma
<?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
输出:第一:yevud nhoj si sihT .dlrow olleh 第二:987654321
回答by Sudhir
You can use it:
你可以使用它:
echo $reversed_s = join(' ',array_reverse(explode(' ',"Hello World")));
回答by Gopal Sharma
public function stringReverse($string="Jai mata di")
{
$length = strlen($string) - 1;
$i = 0;
while ($i < $length + 1) {
echo $string[$length - $i];
$i++;
}
}

