PHP rtrim() Function Explained

The rtrim() function is used to remove whitespace or other specified characters from the end of a string.

string rtrim ( string $str [, string $character_mask ] )

Parameters:

  1. str: Required. Specifies the string to be processed.
  2. character_mask: Optional. Specifies the characters to be removed from the end of the string. Defaults to whitespace.

Original: 我想了解一下这个产品的价格。
Paraphrased: I would like to know the pricing of this product.

$str = "Hello World!   ";
echo rtrim($str); // 输出 Hello World!

$str = "Hello World!***";
echo rtrim($str, "*"); // 输出 Hello World!

In the first example, the rtrim() function removed the spaces at the end of the string. In the second example, the rtrim() function removed the asterisks at the end of the string.

bannerAds