PHP `rtrim` Function Explained: Cleaning Strings from the Right


Purpose

  • It's specifically designed to handle whitespace (spaces, tabs, newlines) by default, but you can also customize it to remove other characters.
  • The rtrim function in PHP is used to remove characters from the right end of a string.

Syntax

rtrim(string $string, string $characters = " \t\n\r\0\x0B");

Parameters

  • characters (optional): This parameter is optional and specifies which characters to remove from the right side of the string. If omitted, it defaults to removing whitespace characters like spaces (), tabs (\t), newlines (\n), carriage returns (\r), NULL (\0), and vertical tab (\x0B). You can provide a string containing the characters you want to eliminate.
  • string: This is the mandatory parameter that represents the string you want to trim.

Return Value

  • The rtrim function returns a new string with the specified characters removed from the right end. If no characters are removed, it returns the original string unchanged.

Example

$original_string = "  Hello, world!   ";

$trimmed_string = rtrim($original_string); // Removes default whitespace
echo $trimmed_string; // Output: "  Hello, world!"

$trimmed_string_custom = rtrim($original_string, "! "); // Removes "!" and spaces
echo $trimmed_string_custom; // Output: "  Hello, world"

Key Points

  • For more complex character removal patterns, consider using regular expressions with preg_replace.
  • rtrim is case-sensitive. It only removes characters that exactly match the ones specified in the characters parameter.
  • To remove characters from both sides, use trim.
  • To remove characters from the left side of a string, use ltrim.


Removing Default Whitespace

$text = "This has   extra spaces  at the end.";
$trimmed_text = rtrim($text);
echo $trimmed_text;  // Output: "This has   extra spaces at the end." (Notice only one space remains)

Removing Specific Characters

$filename = "my_file.txt  "; // Extra spaces and a dot
$clean_filename = rtrim($filename, ". ");  // Remove spaces and dot
echo $clean_filename;  // Output: "my_file.txt"

Removing Newline Characters

$message = "Welcome\nto our website!\n";  // Contains newlines
$clean_message = rtrim($message, "\n");  // Remove only trailing newlines
echo $clean_message;  // Output: "Welcome\nto our website!"

Handling Empty Strings

$empty_string = "";
$trimmed_empty_string = rtrim($empty_string);
echo $trimmed_empty_string;  // Output: "" (Empty string remains empty)
$clean_text = "This string is already clean.";
$still_clean_text = rtrim($clean_text);  // No whitespace to remove
echo $still_clean_text;  // Output: "This string is already clean." (Original string returned)


Using substr

$original_string = "  Hello, world!   ";

$length = strlen($original_string);  // Get the length of the string
$trimmed_string = substr($original_string, 0, $length - strspn($original_string, " \t\n\r\0\x0B"));

echo $trimmed_string; // Output: "  Hello, world!" (Removes default whitespace)
  • This approach uses strlen to determine the string length and then employs substr to extract a substring from the beginning up to the point where the whitespace characters end using strspn.

Regular Expressions with preg_replace

$original_string = "This has   extra spaces     at the end.";

$trimmed_string = preg_replace('/\s+$/', '', $original_string);

echo $trimmed_string; // Output: "This has   extra spaces" (Removes all trailing whitespace)
  • This method utilizes preg_replace with a regular expression (/\s+$/) that matches one or more whitespace characters (\s+) at the end ($) of the string and replaces them with an empty string ('').

Choosing the Right Alternative

  • In edge cases where you need to calculate the number of characters to remove, substr might be a suitable option, but it can be less readable than rtrim.
  • If you need more granular control over the removal pattern (e.g., removing specific characters or non-whitespace characters), regular expressions with preg_replace offer greater flexibility.
  • For simple whitespace removal, rtrim is generally the most efficient choice.
  • Regular expressions can be more complex to write and understand if you're not familiar with them.
  • rtrim is built-in to PHP and doesn't require additional function calls like substr or preg_replace.