PHP rtrim() function

The rtrim() function is used to remove white space and other predefined characters from the end of a string.

Syntax

rtrim(  $string , $character) : string

Parameters

Parameter Name Descrption
$string Required, String to be remove whitespace/predefined character
$character

Optional, The rtrim() function return the string with whitespace removed, if second parameter will not passed then rtrim() will strip these characters:
" " - an ordinary space
"\t" - a tab
"\n" - new line
"\r" - carriage return
"\0" - NUL-byte
"\x0B" - vertical tab.

Return Value

The trimmed string

rtrim() examples

$string1 = "this is string"; //length : 16
$rtrim_string1 = rtrim($string1);
var_dump($rtrim_string1);
//output : 'this is string' (length=14)

$text_rtrim =  "This is string\t\t";
var_dump($text_rtrim); 
//output : 'This is string		' (length=16)

$trimmed = rtrim($text_rtrim);
var_dump($trimmed);
// output : 'This is string' (length=14)

$text_rtrim =  "rtrim function in PHP";
$trimmed = rtrim($text_rtrim,'P');
var_dump($trimmed);
// output : 'rtrim function in PH' (length=20)

Related Functions

  • trim(): Strip whitespace (or other characters) from the beginning and end of a string
  • ltrim(): Strip whitespace (or other characters) from the beginning of a string