Table of Content
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/htmlOutput:
/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?
How can I remove trailing slashes or dots in PHP?
$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 added the array_map() function to help you apply one function to each item in an array. Understand the array_map…
Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…
PHP didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…
Programming is all about choices. Everything you do in code boils down to answering a question: What should happen if…
The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…
Before PHP 5.2, there was no built-in filter extension in PHP. You had to manually handle and sanitize input. PHP…
The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…
The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…
PHP developers relied on class inheritance to share functionality before the OOP interface, but this approach had limits. It solves…
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…