56 of 410 menu

The rtrim Function

The rtrim function removes whitespace from the end of a string. It can also remove other characters if they are specified as the second parameter.

Syntax

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

Example

Let's remove the whitespace from the right:

<?php echo rtrim(' abcde '); ?>

Code execution result:

' abcde'

Example

Let's remove the slashes from the right:

<?php echo rtrim('/abcde/', '/'); ?>

Code execution result:

'/abcde'

Example

Let's remove slashes and dots from the right:

<?php echo rtrim('abcde/', '/.'); ?>

Code execution result:

'abcde'

Example

The function removes any number of the specified characters if they are on the edge:

<?php echo rtrim('abcde/../../', '/.'); ?>

Code execution result:

'abcde'

Example

You can specify a range of characters using two dots '..'. For example, let's specify that we want to remove characters from 'a' to 'd':

<?php echo rtrim('xabcde', 'a..e'); ?>

Code execution result:

'x'

See Also

  • the trim function,
    which removes whitespace from all sides
  • the rtrim function,
    which removes whitespace from the right
  • the chop function,
    which also removes whitespace from the right
byenru