rtrim() Function in PHP 8.1 and 8.2 With Example

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

Hello Friends Today, through this tutorial, I will tell you How to Use `rtrim()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. In PHP 8.1 and 8.2, the `rtrim()` function is used to remove whitespace or other specified characters from the end of a string. Here’s how you can use it with examples:

Syntax:

<?php
rtrim ( string $string , string $characters = " \t\n\r\0\x0B" ) : string
?>

Parameters:

1. `$string`: The input string to be trimmed.
2. `$characters` (optional): The characters you want to remove. If not provided, it will remove whitespace characters.

Return Value: Returns the trimmed string.

Example:

<?php

// Example 1: Removing whitespace from the end of a string
$string1 = "Hello World ";
$result1 = rtrim($string1);
echo "Result 1: '$result1'\n"; // Output: "Result 1: 'Hello World'"

// Example 2: Removing specific characters from the end of a string
$string2 = "Hello World!!!";
$result2 = rtrim($string2, "!");
echo "Result 2: '$result2'\n"; // Output: "Result 2: 'Hello World'"

// Example 3: Using with custom characters
$string3 = "Hello World123";
$result3 = rtrim($string3, "123");
echo "Result 3: '$result3'\n"; // Output: "Result 3: 'Hello World'"

?>

In these examples:

1. Example 1 demonstrates removing whitespace from the end of a string.
2. Example 2 shows removing exclamation marks from the end of a string.
3. Example 3 illustrates using `rtrim()` with custom characters to remove ‘1’, ‘2’, and ‘3’ from the end of the string.

These examples should give you a good understanding of how to use `rtrim()` in PHP 8.1 and 8.2.