PHP 8 reference · String Functions
PHP rtrim() Function
Remove whitespace or other chosen characters from the end (right side) of a string. chop() is an alias.
PHP 8 Since 4.0
Syntax
rtrim(string $string, string $characters = " \n\r\t\v\x00"): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
characters optional |
The characters to strip. Default: " \n\r\t\v\x00" |
Return value
The string without trailing characters.
Examples
<?php
var_dump(rtrim("line of text\r\n"));
echo rtrim("https://example.com///", "/"), "\n";
Output
string(12) "line of text" https://example.com
<?php
$list = "";
foreach (["a", "b", "c"] as $item) {
$list .= "$item, ";
}
echo rtrim($list, ", "), "\n"; // implode() is usually simpler
Output
a, b, c
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
str.rstrip()· Pythonstring.trimEnd()· JavaScript
Related
trim()· Remove whitespace (or other characters you choose) from both ends of a string. Spaces inside the string are kept.ltrim()· Remove whitespace or other chosen characters from the start (left side) of a string only.implode()· Join array elements into one string with a separator between them. join() is an alias of implode().- PHP Strings · tutorial