PHP rtrim() Function (original) (raw)

Last Updated : 23 Sep, 2024

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,string, 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 detail below :

**Return Value:

**Examples:

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

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

Examples of PHP rtrim() Function

Below programs will illustrate rtrim() function:

**Example 1: Using rtrim() Without a Character List

In this example, the rtrim() function is used to remove trailing whitespaces from the end of a string. No specific character list is provided, so the function will automatically remove default whitespace characters such as spaces, tabs, and newline characters.

php `

`

**Example 2: Using rtrim() with a Specified Character List

In this example, the rtrim() function is used with a specified character list, where specific characters like !, (, and ) are removed from the right side of the string.

php `

`