rtrim
Removes whitespace or given characters from the end of a string.
Syntax
rtrim(string $string, string $characters = " \n\r\t\v\0"): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
| $characters | string | Optional. Which characters to remove. |
Return value
stringThe string without the extra characters on the right.
Examples
A line break at the end:
<?php
echo '[' . rtrim("line\n") . ']';Output:
[line]A trailing slash in an address:
<?php
echo rtrim('https://example.com/', '/');Output:
https://example.comNotes
Handy for lines read from a file: each keeps its trailing line break.
See also
In the tutorial
- Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.