PHP | rtrim() Function

The rtrim() function is a built-in function in PHP which removes whitespaces or other characters (if specified) from the right side of a string.

Syntax:

rtrim( $string, $charlist )

Parameters: The function rtrim() accepts two parameters as shown in the above syntax. Out of these two parameters, one is mandatory while the other one is optional. They are discussed in details below :

  • $string: This mandatory parameter specifies the string to be checked.
  • $charlist: This optional parameter specifies which characters are to be removed from the string.In case, this parameter is not provided, the following characters are removed :
    • “\0” – NULL
    • “\t” – tab
    • “\n” – new line
    • “\x0B” – vertical tab
    • “\r” – carriage return
    • ” “ – ordinary white space

Return Value: Returns the modified string.

Examples:

Input : $string = "Geeks for Geeks    "
Output : Geeks for Geeks

Input : $string = "Geeks for !!! (( !!))", $charlist = "! ()"
Output : Geeks for 

Below programs will illustrate rtrim() function :

Program 1: This program shows the use of rtrim() function without any specified list of characters to be removed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$string = "Geeks for    ";
echo rtrim($string)." Geeks";
  
?>

chevron_right


Output:

Geeks for Geeks

Program 2: This program shows the use of rtrim() function with a specified list of characters to be removed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$string = "Geeks for !!! (( !!))";
  
// The characters '!', '(', ')', ' ' have been specified 
// to remove from the end of the string
echo rtrim($string, "! ()")." Geeks";
  
?>

chevron_right


Output:

Geeks for Geeks

Reference:
http://php.net/manual/en/function.rtrim.php



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.