PHP 8.0.0 Beta 4 available for testing

strrev

(PHP 4, PHP 5, PHP 7)

strrev文字列を逆順にする

説明

strrev ( string $string ) : string

string を逆順にして返します。

パラメータ

string

逆順にしたい文字列。

返り値

逆順にした文字列を返します。

例1 strrev() で文字列を逆順にする

<?php
echo strrev("Hello world!"); // 出力は "!dlrow olleH" となります
?>

add a note add a note

User Contributed Notes 3 notes

up
13
tianyiw at vip dot qq dot com
2 years ago
This function support utf-8 encoding, Human Language and Character Encoding Support:

<?php
function mb_strrev($str){
   
$r = '';
    for (
$i = mb_strlen($str); $i>=0; $i--) {
       
$r .= mb_substr($str, $i, 1);
    }
    return
$r;
}

echo
mb_strrev("☆❤world"); // echo "dlrow❤☆"
?>
up
1
info at ensostudio dot ru
26 days ago
<?php
/**
* Reverse a miltibyte string.
*
* @param string $string The string to be reversed.
* @param string|null $encoding The character encoding. If it is omitted, the internal character encoding value
*     will be used.
* @return string The reversed string
*/
function mb_strrev(string $string, string $encoding = null): string
{
   
$chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding());
    return
implode('', array_reverse($chars));
}
?>
It's faster and flexible than tianyiw function (comment #122953)
up
-27
arturklesun at gmail dot com
2 years ago
Be careful, it does not work with unicode strings.

<?php
php
> $str = '1¥';
php > print($str);

php
> print(strrev($str));
��1
?>
To Top