English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP rtrim() Function Usage and Example

   PHP String String Functions Manual

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

Syntax

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

Definition and Usage

Used to remove trailing spaces from the string

Related Functions:

  • ltrim() - Remove whitespace characters or other predefined characters from the beginning of the string.

  • trim() - Remove whitespace characters or other predefined characters from both sides of the string.

Return Value

It returns the modified string

Parameter

NumberParameter and Description
1

string

Input String

2

character_mask

Specify which characters to remove from the string. If the parameter is omitted, the following characters will be removed by default:
  • "\0" - NULL

  • "\t" - Tab

  • "\n" - New line

  • "\x0B" - Vertical tab

  • "\r" - Carriage return

  • " " - Space

Online Example

Try the following examples, remove trailing spaces and remove the specified string from the end of the string:

<?php
   $input = "hi sai ";
   
   //Remove trailing spaces from the string
   echo rtrim($input) . "<br>";
   
   //Remove the specified string 'sai' from the end of the string
   echo rtrim($input,"sai");
?>
Test and see‹/›

Output Result

hi sai
hi

PHP String String Functions Manual