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

Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

How to Remove the Last Character from a String in PHP

You may need to cut the last character from a string if it shows by mistake in PHP. That may…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

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 Magic Constants: Simplify Your Code

There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…

How to Insert Multiple Rows in MySQL with PHP?

Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…

PHP fwrite: How to Write Data to Files in PHP

actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

PHP array_combine: How to Merge Arrays as Key and Value

The array_combine function in PHP creates a new array with one array for keys and another for values. It needs…

PHP MySQL ORDER BY: How to Sort Data in SQL?

Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…

PHP sizeof: How to Count Elements in Arrays with Examples

The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

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.