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


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *