Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

rtrim

Removes whitespace or given characters from the end of a string.

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe source string.
$charactersstringOptional. 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.com

Notes

Handy for lines read from a file: each keeps its trailing line break.

See also

  • trim — Removes whitespace and other characters from both ends of a string.
  • ltrim — Removes whitespace or given characters from the start of a string.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.