International PHP Conference Berlin 2025

strrev

(PHP 4, PHP 5, PHP 7, PHP 8)

strrev反转字符串

说明

strrev(string $string): string

返回 string 反转后的字符串。

参数

string

待反转的原始字符串。

返回值

返回反转后的字符串。

示例

示例 #1 使用 strrev() 反转字符串

<?php
echo strrev("Hello world!"); // 输出 "!dlrow olleH"
?>

添加备注

用户贡献的备注 2 notes

up
11
info at ensostudio dot ru
4 years 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
7
tianyiw at vip dot qq dot com
6 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❤☆"
?>
To Top