strrev(): PHP String Reverse

echo strrev('plus2net'); //ten2sulp
Syntax
echo strrev($input_string);
$input_string: Input string to be reversed
Output is the string after reversing input string.

The original ( input string ) remains same.

We can reverse any string by applying strrev() function.
Here is the code
$st="This is a test";
echo "The string = ".$st."<br>";
$st=strrev($st);
echo "The new string after applying the strrev function = ".$st."<br>";

Printing the string in reverse without using strrev() function.

Here is the code.
<?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.

Palindrome

A string is palindrome if reverse of the string is same as main string.
Check the string or number is Palindrome or not in PHP

Example 2: Reversing a String Without Affecting Special Characters

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

Example 3: Reversing Only Words in a Sentence

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

STRING REFERENCE strlen(): Length of the string
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    huzoor

    23-04-2012

    Nice Collection.. I feel very happy




    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer