rtrim() Function Use in PHP 8.5 and 8.4

The rtrim() function in PHP 8.5 (and 8.4) works exactly as it always has: it strips whitespace or other specified characters from the end of a string.

Usage.

By default, it removes spaces, tabs, newlines, carriage returns, NUL bytes, and vertical tabs.

<?php

$text = "Hello World   ";

echo rtrim($text); 

// Outputs: "Hello World"

?>

Characters.

You can specify exactly which characters to strip by passing a second argument. It also supports character ranges using..

<?php
// Remove specific characters
echo rtrim("Hello World...", " ."); // Outputs: "Hello World"

// Remove a range of control characters
$clean = rtrim("x09Example stringx0A", "x00..x1F");
?>

Key Points for PHP 8.4/8.5.

  • No Changes: The function signature and behavior are stable; there are no version-specific deprecations or new features in 8.4/8.5.
  • Character Mask: Remember that the second parameter is a character mask, not a substring. It removes any of the listed characters individually, not the exact sequence.
  • Multibyte Alternative: If you are working with UTF-8 strings and need to trim multibyte whitespace (like non-breaking spaces), you might prefer the newer mb_rtrim()function available since PHP 8.4.


 

26 Views