PHP rtrim Function: Remove Trailing Characters or Whitespace

PHP trtrim Function

You deal with user input, file reads, or formatted strings. PHP needed a way to remove extra characters from the end of a string without touching the rest. That is where rtrim() came in.

Understand the rtrim Function in PHP

The rtrim() function removes characters from the end of a string. You can use it to clean up trailing spaces, tabs, newlines, or any set of characters you choose.

Here is the syntax:

rtrim( $string, $characters )

Parameters:

  • $string — This is the input string you want to trim.
  • $characters (optional) — This is a list of characters you want PHP to remove from the end. If you skip this, PHP uses a default set of whitespace characters.

It returns a new string with the chosen characters removed from the right side. It does not change the original string.

The rtrim() function has been part of PHP from the start. You can use it in every stable version, including:

  • PHP 4
  • PHP 5
  • PHP 7
  • PHP 8 (up to 8.3)

You do not need extensions, and it works the same across versions.

Here is a quick example:

$price = "199.99$$$";
$cleanPrice = rtrim($price, '$');

echo $cleanPrice; 

Output:

199.99

This removes all trailing dollar signs from the end of the string. It only trims the end, not the start or middle.

Default Behavior (Removing Whitespace) with PHP rtrim Function

When you call rtrim() with just one argument, it removes whitespace from the end of the string. This includes:

  • Spaces
  • Tabs (\t)
  • Newlines (\n)
  • Carriage returns (\r)
  • Null bytes (\0)
  • Vertical tabs (\v)

Here is a simple example:

$text = "hello world   ";
$cleaned = rtrim($text);
echo $cleaned;  

Output:

hello world

PHP leaves the text untouched except for the trailing spaces.

Use PHP rtrim Function with Custom Character Mask

You can pass a second argument to tell rtrim() what to remove. This argument is a character mask, not a full string. PHP treats each character in it as a single match.

Here is an example that trims dots and dashes:

$line = "file-name.txt....---";
$clean = rtrim($line, ".-");
echo $clean;

Output:

file-name.txt

You can remove letters, symbols, or any mix. PHP reads each character one by one from the right side and stops when the next character does not match the list.

Examples of rtrim() Function in PHP

Remove whitespace:

$value = "user input   ";
echo "[" . rtrim($value) . "]";

Output:

[user input]

This code removes extra spaces from the end of the string "user input ". It then shows the trimmed result inside square brackets: [user input].

Trim specific characters:

$url = "example.com///";
echo rtrim($url, "/");

Output:

example.com

This removes all slashes from the end of the string “example.com///”. The output becomes: example.com — only the end slashes go, not anything in the middle.

Clean file path endings:

$path = "/var/www/html///";
$fixed = rtrim($path, "/");
echo $fixed; // /var/www/html

Output:

/var/www/html

This trims all trailing slashes from the path string “/var/www/html///”. The result is “/var/www/html” — it keeps the rest of the path unchanged.

Trim trailing letters:

$data = "ReportAAA";
echo rtrim($data, "A");

Output:

Report

This removes all A characters from the end of the string “ReportAAA”. The output is “Report” — it stops once it hits a different character.

Wrapping Up

The rtrim() function gives you a simple way to clean up the right side of a string. It works with whitespace or any custom set of characters.

You can use it to fix file paths, URLs, or raw input. It does not change the original string and works in all PHP versions.

FAQs

What does PHP rtrim() do and how does it work?

The rtrim() function in PHP removes characters from the end of a string. By default, it removes whitespace characters like spaces, tabs, newlines, and null bytes. You can also pass a custom character mask to remove specific characters from the right side. It returns a new string and leaves the original string unchanged.

How can I remove trailing slashes or dots in PHP?

You can use rtrim() with a custom character mask to remove trailing slashes or dots. For example:
$url = "example.com///";
echo rtrim($url, "/"); // "example.com"
This removes all slashes from the end of the string. You can also remove dots by passing "." or any combination like ".-/".

Similar Reads

PHP array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

PHP Comment: How to Write Comments in PHP

let's now dive into PHP comments—these simple but super important integral parts of PHP coding. You can think of comments…

PHP While Loop: Iterating Through Code Blocks

The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…

PHP SimpleXML: Work with XML in Examples

PHP SimpleXML - invented for you to work with XML. Way leaner, quicker to get up. it's already integrated directly…

PHP AND (&&) Operator: Usage & Examples

As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…

PHP Anonymous Function: How It Works with Examples

The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

PHP Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

Previous Article

What Is AJAX? How It Works in Web Development with Examples

Next Article

JavaScript Hoisting: How It Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.