📅  最后修改于: 2023-12-03 15:04:56.465000             🧑  作者: Mango
The rtrim
function is used to remove whitespace or other specified characters from the end of a string. It stands for "right trim" because it only affects the right (or trailing) end of the string.
The syntax for the rtrim
function is as follows:
rtrim(string $string, ?string $charlist = " \t\n\r\0\x0B"): string
The function takes two parameters:
string
: The input string to be trimmed.charlist
: An optional parameter specifying which characters to remove. If omitted, the default list of whitespace characters is used.The function returns the trimmed string.
Here are some examples of how to use the rtrim
function:
// Remove trailing whitespace
$str1 = "Hello World ";
$str1 = rtrim($str1); // "Hello World"
// Remove trailing specific characters
$str2 = "This is a sentence.";
$str2 = rtrim($str2, "."); // "This is a sentence"
// Remove trailing whitespace and specific characters
$str3 = " abc.def. \t";
$str3 = rtrim($str3, ". \t"); // " abc.def."
The rtrim
function is a useful tool for manipulating strings in PHP programs. By removing trailing whitespace or other specified characters, it helps to ensure that strings are properly formatted and ready for further processing.