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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

rtrim(string $string, string $characters = " \n\r\t\v\x00"): string

Parameters

ParameterDescription
string The input string.
characters optional The characters to strip. Default: " \n\r\t\v\x00"

Return value

The string without trailing characters.

Examples

Strip from the rightTry it yourself
<?php
var_dump(rtrim("line of text\r\n"));
echo rtrim("https://example.com///", "/"), "\n";

Output

string(12) "line of text"
https://example.com
Remove a trailing separatorTry it yourself
<?php
$list = "";
foreach (["a", "b", "c"] as $item) {
    $list .= "$item, ";
}
echo rtrim($list, ", "), "\n"; // implode() is usually simpler

Output

a, b, c

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?