©
This document uses PHP Chinese website manual Release
(PHP 4, PHP 5)
strrev — 反转字符串
$string
)
返回 string
反转后的字符串。
string
待反转的原始字符串。
返回反转后的字符串。
Example #1 使用 strrev() 反转字符串
<?php
echo strrev ( "Hello world!" ); // 输出 "!dlrow olleH"
?>
[#1] Anonymous [2015-04-09 17:53:50]
My version
<?php
$var= "Hello, hello. ";
$length = strlen($var)-1;
$i = 0;
while ($i < $length+1) {
echo $var[$length-$i];
$i++;
}
[#2] vsrikanth02 at gmail dot com [2014-04-21 10:55:59]
$string = 'srikanth';
$length = strlen($string);
for($i=$length-1;$i >=0;$i--){
echo $string[$i];
}
[#3] vsrikanth02 at gmail dot com [2014-04-21 10:54:50]
$string = 'srikanth';
$length = strlen($string);
for($i=$length-1;$i >=0;$i--){
echo $string[$i];
}
[#4] sheik dot ks at gmail dot com [2014-04-21 09:07:17]
$str = "sheik";
$arr1 = str_split($str);
$revstr='';
$i=count($arr1);
while($i >= 0){
$revstr .= $arr1[$i];
$i--;
}
echo $revstr;
[#5] dthawkins+php at gmail dot com [2014-04-08 18:18:35]
Reverse a string without using any functions
function reverse_string($s) {
$i = 0;
$rev = '';
while ( $s[$i] ) {
$i++;
}
$i--;
while ( $s[$i] ) {
$rev .= $s[$i];
$i--;
}
return $rev;
}
[#6] manfred at werkzeugH dot at [2008-05-27 04:35:07]
here is my version for strings with utf8-characters represented as numerical entities (e.g. Ӓ)
function utf8_entities_strrev($str, $preserve_numbers = true)
{
//split string into string-portions (1 byte characters, numerical entitiesor numbers)
$parts=Array();
while ($str)
{
if ($preserve_numbers && preg_match('/^([0-9]+)(.*)$/',$str,$m))
{
// number-flow
$parts[]=$m[1];
$str=$m[2];
}
elseif (preg_match('/^(\&#[0-9]+;)(.*)$/',$str,$m))
{
// numerical entity
$parts[]=$m[1];
$str=$m[2];
}
else
{
$parts[]=substr($str,0,1);
$str=substr($str,1);
}
}
$str=implode(array_reverse($parts),"");
return $str;
}
[#7] carmel.alex at gmail.com [2006-02-28 00:54:32]
This function support utf-8 encoding
function utf8_strrev($str){
preg_match_all('/./us', $str, $ar);
return join('',array_reverse($ar[0]));
}
[#8] MagicalTux at FF dot st [2005-05-12 06:20:23]
I will make Screend at hf dot webex dot com's comment more clear and understandable.
strrev only works for singlebyte character-sets. Multibytes charactersets are not compatibles with strrev.
US-ASCII and ISO-8859-* are compatible with strrev, however BIG5, SJIS, UTF-8 aren't.
There's no mb_strrev function in PHP, so you can't strrev() a multibyte string. Try to convert it to something else with iconv() if it can be represented in a singlebyte character set.