PHP rtrim() Function

What is PHP rtrim() Function?

To remove characters and whitespaces from right side of a string, use rtrim() function. “rtrim” is short for “Right Trim”. Removing whitespaces from a string is a very common and necessary task in web development. For example, a user may add an extra space mistakenly at the end of his username Later, when he’ll try to login with the username, he can’t do it because he will just try with the username except the space.

Note, a whitespace is any character that renders a space. It includes –

  • An ordinary space  – “ ”
  • A tab – “\t”
  • A new line (line feed) – “\n”
  • A carriage return – “\r”
  • The NUL-byte – “\0”
  • A vertical tab – “\t”

Syntax:

rtrim(string, characters)

Parameters:

The Function has 1 required and 1 optional parameter-

string (Required): The string that is to be trimmed. Check example 1. If you don’t mention the second parameter, the rtrim() function actually removes the following characters-

  • An ordinary space  – “ ”
  • A tab – “\t”
  • A new line (line feed) – “\n”
  • A carriage return – “\r”
  • The NUL-byte – “\0”
  • A vertical tab – “\t”

characters (Optional): A list of characters that is to be removed from the “string” parameter.

  • Mention all the characters that you want to trim from the string. Check example 2.
  • If you want to remove a range of characters, use “..”.  Ex. “1..5”, “A..M” etc. For example, if you want to remove all the character from a to m, specify “a..m”. Check example 3.

Return Values:

The function returns the trimmed string.

How rtrim() function works:

When using with just one parameter (the string), the function removes any space, tab, new line, carriage return, NUL-byte, vertical tab from right side of the string.

When using with the second parameter (the “characters”), it removes all the characters from right side of the first parameter. The function removes the characters listed in second parameter recursively from right side of the string as long as it finds any mismatching characters. Check example 4.

Examples:

Example 1: Trimming whitespaces with rtrim() function-

<pre>
<?php
$string   = "Learn PHP at SOW  ";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string);
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn PHP at SOW  ‘
After trimming: ‘Learn PHP at SOW’

Explanation:

When rtrim() function uses just one parameter, it removes all the whitespaces including space in the example above from the string.

Example 2: Trimming specified characters with rtrim() function-

<pre>
<?php
$string   = "Learn PHP at SOW now.";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, " own.");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn PHP at SOW now.’
After trimming: ‘Learn PHP at SOW’

Explanation:

The function removes 5 characters from the right side of the string – space, “o”, “w”, “n”, and dot(.).

Example 3: Trimming a list of characters with rtrim() function-

<pre>
<?php
$string   = "Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, "A..Z ");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT’
After trimming: ‘Learn web develpment at Schools of web:’

Explanation:

The function removes all the capital English letters from the end of the string.

Example 4: Trimming characters recursively with rtrim() function-

<pre>
<?php
$string   = "PHP Web Development xzxcc";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, "cxz");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘PHP Web Development xzxcc’
After trimming: ‘PHP Web Development ‘

Explanation:

The function trims 3 characters from the end of the string – “c”, “x”, and “z”. After removing “c” from the end of the string, the function finds the “c” again at the end of the string. The function again checks if this “c” is in the second list. As it does, it trims this character and it continues until it finds any mismatching.

Example 5: Trimming similar type of characters from a pile of data with rtrim() function-

<pre>
<?php
$languages   = array("PHP1994", "Python1991", "Ruby1995");
for($i=0; $i<count($languages); $i++){
    echo "Before trimming: '$languages[$i]'";
    $afterTrimming = rtrim($languages[$i], "0..9");
    echo " After trimming: '$afterTrimming'" . "<br />";
}
?>
</pre>

Output:

Before trimming: ‘PHP1994’ After trimming: ‘PHP’
Before trimming: ‘Python1991’ After trimming: ‘Python’
Before trimming: ‘Ruby1995’ After trimming: ‘Ruby’

Explanation:

All the array elements have same pattern – numbers at last. So, to trim these numbers, you can mention the numbers (here 0..9) to remove in the second parameter.

Example 6: Trimming whitespaces from the second parameter with rtrim() function-

<pre>
<?php
$string   = "Learn HTML5 at Schools of Web\t\n\r  ";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = trim($string, "a");
echo "After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'". "<br />";
$afterTrimming = trim($string, " \n\r\t");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn HTML5 at Schools of Web         

  ‘
 After trimming: ‘Learn HTML5 at Schools of Web            

  ‘
Before trimming: ‘Learn HTML5 at Schools of Web          

  ‘
After trimming: ‘Learn HTML5 at Schools of Web’

Explanation:

As the function uses second parameter, it doesn’t remove the whitespace characters automatically at the end of the string. That’s why we mention the whitespaces to remove in the second parameter.

Example 7: Trimming different case characters with rtrim() function-

<pre>
<?php
$string   = "Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, "react mysql");
echo "After trimming: '$afterTrimming'";
echo "<br />";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, "REACT MYSQL");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT’
After trimming: ‘Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT’
Before trimming: ‘Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP MYSQL REACT’
After trimming: ‘Learn web develpment at Schools of web: HTML CSS JAVASCRIPT PHP’

Explanation:

As we want to trim capital letters at the end of the string, mentioning small letters can’t trim those. That’s why, rtrim() at line 5 can’t trim, but it does at line 9.

Example 8: Trimming multiple characters at a time with rtrim() function-

<pre>
<?php
$string   = "Learn web development at Schools of web 1234 XYZ \t";
echo "Before trimming: '$string'" . "<br />";
$afterTrimming = rtrim($string, "A..Z, 1..9, ' ','\t'");
echo "After trimming: '$afterTrimming'";
?>
</pre>

Output:

Before trimming: ‘Learn web development at Schools of web 1234 XYZ ‘
After trimming: ‘Learn web development at Schools of web’

Explanation:

To trim numbers, whitespace characters, English letters from the string, we mention them with comma separated in the second parameter.

Practical Usages of rtrim() Function:

  • The function is very useful to sanitize user inputs. It eliminates unnecessary spaces, tabs, carriage returns, newline etc. from user inputs. For example, users may add extra space when supplying username or add line break at the end of a comment. This function helps to trim these characters.
  • The function is also useful to sanitize data that we receive from external files, database etc.
  • To trim similar types of characters from the right side of the string, you can use rtrim() function. For example, you have a large excel file which has a string column. Each string of the column contains few number on its right side. You use rtrim() to easily remove numbers. Check example 5.

Notes on rtrim() Function:

  • When you mention the second parameter, the rtrim() function doesn’t remove the whitespaces from the string automatically. In this case, mention the whitespace characters that you want to remove. Check example 6.
  • The characters you mention in the second parameter work case-sensitively. Which means, if you enlist “S” (Capital letter), the function will remove “S” (Capital letter) not “s” (Small letter) from string. Check example 7.
  • In the second parameter, to remove multiple characters at a time list them separated with comma: English letters, numbers, multiple whitespace characters. Check example 8.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP rtrim() Function

Use the rtrim() function when you need to trim characters at the end of the string. It is another frequently used built-in PHP string functions that help you sanitize user inputs and ensure data consistency.

Reference:

https://www.php.net/manual/en/function.rtrim.php