strrev() reverses a string byte by byte. It is straightforward for simple ASCII text but is not multibyte-aware for general UTF-8 text.
$text = 'plus2net';
echo strrev($text); // ten2sulp
Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.
echo strrev('plus2net'); //ten2sulp
Syntax
echo strrev($input_string);
$input_string: Input string to be reversed$st="This is a test";
echo "The string = ".$st."<br>";
$st=strrev($st);
echo "The new string after applying the strrev function = ".$st."<br>";
<?Php
$st="Hello world";
//echo $st[4];
$length=strlen($st);
for($i=$length-1;$i>=0;$i--){
echo $st[$i];
}
?>
Output is here
dlrow olleH
This function support utf-8 encoding and does not work with unicode strings.
Using *strrev()* can reverse strings, but it also affects special characters. Here's how to reverse a string while preserving HTML entities like < and >:
$str = "Hello <World>";
$reversed_str = htmlspecialchars(strrev(htmlspecialchars_decode($str)));
echo $reversed_str;
// Outputs: >dlroW< olleH
This example demonstrates reversing only the words while keeping their order intact:
$sentence = "PHP is great";
$reversed_words = implode(' ', array_reverse(explode(' ', $sentence)));
echo $reversed_words;
// Outputs: great is PHP
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| huzoor | 23-04-2012 |
| Nice Collection.. I feel very happy | |