PHP rtrim() Function
Example
Remove characters from the right side of a string:
"; echo rtrim($str,"World!"); ?>Run Example »
Definition and Usage
The rtrim() function removes whitespace or other predefined characters from the right side of a string.
Related functions:
- ltrim() - Remove whitespace or other predefined characters from the left side of a string.
- trim() - Remove whitespace or other predefined characters from both sides of a string.
Syntax
rtrim(
string,charlist
)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| charlist | Optional. Specifies which characters to remove from the string. If this parameter is omitted, all of the following characters are removed:
|
Technical Details
| Return Value: | Returns the modified string. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 4.1, charlist parameter was added. |
More examples
Example 1
Remove spaces from the right side of a string:
"; echo "With rtrim: " . rtrim($str); ?>The HTML output of the above code is as follows (view source):
Without rtrim: Hello World!
With rtrim: Hello World!
The browser output of the above code is as follows:
Without rtrim: Hello World!
With rtrim: Hello World!
Run Example »
Example 2
Remove the newline character (\n) from the right side of a string:
"; echo "With rtrim: " . rtrim($str); ?>The HTML output of the above code is as follows (view source):
Without rtrim: Hello World!
With rtrim: Hello World!
The browser output of the above code is as follows:
Without rtrim: Hello World!
With rtrim: Hello World!
Run Example »
PHP String Reference Manual