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.
Table of Content
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?
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
The PHP associative array is a structured collection wherein data elements are organized into lists or groups. Each element is…
Perhaps you're new to PHP or sharpening your skills and looking to understand how numbers work within this language under…
Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…
Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…
Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…
Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…
PHP has a number of predefined constants that give you some information about the language and its environment. You can…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
Type casting in PHP refers to the process of converting a variable from one data type to another. This is…
When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…