String Functions
Advertisements

PHP rtrim() Function

Topic: PHP String ReferencePrev|Next

Description

The rtrim() function strips whitespace and other characters from the end of a string.

The following table summarizes the technical details of this function.

Return Value: Returns the trimmed string.
Version: PHP 4+

Syntax

The basic syntax of the rtrim() function is given with:

rtrim(string, strip_characters);

The following example shows the rtrim() function in action.

<?php
// Sample string
$str = "  Hello World! ";

// Stripping whitespace from the end
echo rtrim($str);
?>

Parameters

The rtrim() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to be trimmed.
strip_characters

Optional. Specifies the list of all characters that you want to be stripped.

If this parameter is not specified, rtrim() will strip all the following characters:

  • " " - an ordinary space
  • "\t" - a tab
  • "\n" - a new line
  • "\r" - a carriage return
  • "\0" - the NULL-byte
  • "\v" - a vertical tab

More Examples

Here're some more examples showing how rtrim() function actually works:

In the following example only space and new line characters from the end of the string will be stripped. Whitespace characters at the beginning and inside of the string will remain intact.

<?php
// Sample string
$str = "  \tHello\n World!\n ";

// Trimming the trailing whitespace
echo rtrim($str);
?>

In the following example only space, plus, and minus from right end of the string will be removed.

<?php
// Sample string
$str = "  ++--\tHello World!++-- ";

// Removing characters from the right side
echo rtrim($str, " +-");
?>
Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties