Categories: PHP

rtrim – PHP String Functions

Syntax :

rtrim ( string, charlist );

Description :

rtrim() function will truncate spaces or other characters from the end of input string. We can remove “es.” from the end of input string “hi from tutorialmines.” by this function.


Parameter :

  • string – This is a Required parameter. This is a string on which function will be performed.
  • charlist – This is a Optional parameter. If you wont use the second parameter, rtrim() will strip below characters:

The following characters are removed if the charlist parameter is empty:

  • “\0” – NULL
  • “\t” – tab
  • “\n” – new line
  • “\r” – carriage return
  • ” ” –  ordinary white space
  • “\x0B” – vertical tab

Output :

This will return a trimmed string.


Related articles : trim , ltrim.


rtrim() – PHP Functions Example 1 :
<?php
// below statement will trim white spaces from right side of string."
$strExample = "Hi from tutorialmines! "; 
echo rtrim($strExample);
?>

In above example ,We have a string “Hi from tutorialmines! “. Now this function will trim blank spaces from right side of string. Output of above code in the browser is as below:

Hi from tutorialmines!

rtrim() – PHP Functions Example 2 :
<?php
// below statement will trim white spaces from right side of string."
$strExample = " \nHi from \ntutorialmines.\n\n ";
echo rtrim($strExample);
?>

In above example ,We have a string ‘ \nHi from \ntutorialmines.\n\n ‘. Now this function will trim blank spaces from right side of string. Output in ViewSource of browser is below ( HTML output of the code above):

<html><head></head><body>
 
Hi from 
tutorialmines.

</body></html>

See below is the output of above code.

Hi from tutorialmines.

rtrim() – PHP Functions Example 3 : Using charlist parameter.
<?php
$strExample = "Hi from tutorialmines. This is an example of rtrim() function. Testing parameter."; 
echo rtrim($strExample,"parameter.");
?>

In above example ,We have a string ‘Hi from tutorialmines. This is an example of trim() function. Testing parameter.’. Now this function remove “parameter.” from the end of string. See below is the output of above code.

Hi from tutorialmines. This is a example of rtrim() function. Testing


rtrim() – PHP Functions Example 4 : Using charlist parameter with (..) specifying range of characters.
<?php
$strExample = "Hi from tutorialmines. This is an example of rtrim() function. Testing parameter123."; 
echo rtrim($strExample,"a..z.0..9");
?>

In above example ,We have a string ‘Hi from tutorialmines. This is an example of rtrim() function. Testing parameter.’. Now this function remove “parameter123.” character from the end of string by using range of characters with .. operator.

  • 0..9 – it will remove numbers from 0-9 from right side.
  • a..z – it will remove alphabets from a to z  from right side.
  • . – it will remove dot ( . ) from right side.

See below is the output of above code.

Hi from tutorialmines. This is an example of rtrim() function. Testing


jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

6 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

6 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

6 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

6 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

6 months ago