Current Location: Home> Function Categories> rtrim

rtrim

Delete whitespace characters (or other characters) at the end of the string
Name:rtrim
Category:String
Programming Language:php
One-line Description:Removes whitespace or other characters to the right of the string.

Definition and usage

rtrim() function removes whitespace characters or other predefined characters to the right of the string.

Related functions:

  • ltrim() - Removes whitespace or other predefined characters to the left of the string
  • trim() - Remove whitespace or other predefined characters on both sides of the string

Example

Example 1

Remove characters from the right side of the string:

 <?php
$str = "Hello World!" ;
echo $str . "<br>" ;
echo rtrim ( $str , "World!" ) ;
?>

Try it yourself

Example 2

Remove the space to the right of the string:

 <?php
$str = "Hello World!" ;
echo "Don't use rtrim:" . $str ;
echo "<br>" ;
echo "Use rtrim:" . rtrim ( $str ) ;
?>

The HTML output of the above code is as follows (see the source code):

 < ! DOCTYPE html >
< html >
< body >

Don't use rtrim: Hello World ! < br > Use rtrim: Hello World !
< / body >
< / html >

The browser output of the above code:

 Don't use rtrim: Hello World!
Use rtrim: Hello World!

Try it yourself

Example 3

Remove the newline character (\n) to the right of the string:

 <?php
$str = "Hello World!\n\n\n" ;
echo "Don't use rtrim:" . $str ;
echo "<br>" ;
echo "Use rtrim:" . rtrim ( $str ) ;
?>

The HTML output of the above code is as follows (see the source code):

 < ! DOCTYPE html >
< html >
< body >

Don't use rtrim: Hello World !


< br > Use rtrim: Hello World !
< / body >
< / html >

The browser output of the above code:

 Don't use rtrim: Hello World!
Use rtrim: Hello World!

Try it yourself

grammar

 rtrim ( string , charlist )
parameter describe
string Required. Specifies the string to be checked.
charlist

Optional. Specifies which characters to delete from the string. If omitted, remove all the following characters:

  • "\0" - NULL
  • "\t" - Tab
  • "\n" - Line break
  • "\x0B" - Vertical tab character
  • "\r" - Enter
  • " " - Spaces
Similar Functions
Popular Articles