PHP rtrim() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, rtrim() removes whitespace, or other characters you choose, from the end of a string.

The start of the string is left alone. chop() is an alias of rtrim().

Syntax

Call rtrim() with the values you need. Start with this line.

PHP
$clean = rtrim("Hello   ");
CallWhat it does
rtrim("Hello ")Returns "Hello".
rtrim("1.500", "0")Returns "1.5".
rtrim("dir///", "/")Returns "dir".

The second argument is a list of characters. Each one is removed from the right until a character not in the list is found.

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

Parameters

ParameterDescription
stringRequired. The input string.
charactersOptional. Characters to strip. Defaults to space, tab, newline, carriage return, vertical tab, and the NUL byte. Use a..z style ranges for sets.

Return Value

Return ValueDescription
stringThe string with the matching characters removed from the end.

PHP Examples

Example 1 — Remove Trailing Spaces

The everyday use: clean up spaces and new lines at the end of text.

PHP
<?php
$text = "Hello, CodeToFun   ";
$clean = rtrim($text);

echo "[" . $clean . "]", PHP_EOL;

How It Works

  1. Start with trailing spaces. Three spaces end the text.
  2. Call rtrim with one argument. It uses the default whitespace list.
  3. Spaces are removed. Stripping stops at the last n.
  4. Brackets show the edges. There is no space before the closing bracket.

Example 2 — Trim Zeros and a Dot

A classic trick for cleaning decimal strings, and a range for digits.

PHP
<?php
echo rtrim("1.500", "0"), PHP_EOL;
echo rtrim("3.000", "0"), PHP_EOL;
echo rtrim(rtrim("3.000", "0"), "."), PHP_EOL;
echo rtrim("abc123", "0..9"), PHP_EOL;

How It Works

  1. Strip zeros. 1.500 becomes 1.5.
  2. The dot stays. 3.000 becomes 3. with a dot.
  3. Strip the dot as well. A second rtrim fixes that.
  4. Use a range. 0..9 removes all trailing digits.

Example 3 — Clean Up a Path and a Line

Remove trailing slashes from a folder path, and a newline from a line of text.

PHP
<?php
$path = "path/to/dir///";
echo rtrim($path, "/"), PHP_EOL;

$line = "first line\n";
echo "[" . rtrim($line) . "]", PHP_EOL;

How It Works

  1. Strip slashes. All three trailing slashes are removed.
  2. Keep inner slashes. Only the end is touched.
  3. Remove a newline. The default list includes \n.
  4. Common with files. Lines read from a file often end with a newline.

Frequently Asked Questions

rtrim() removes whitespace or a set of characters you choose from the end of a string and returns the result.
string rtrim(string $string, string $characters = " \n\r\t\v\x00"). The characters argument is optional.
Yes. chop() is an alias of rtrim(). Prefer rtrim() because it is clearer.
No. It treats the second argument as a list of characters, not a word. Use str_ends_with() and substr() to remove an exact suffix.
rtrim() cleans only the end, ltrim() cleans only the start, and trim() cleans both.

Next: setlocale()

setlocale() sets the locale used for things like number formatting, dates, and character t.

setlocale() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful