String Functions

PHP chop() Function

Topic: PHP String ReferencePrev|Next

Description

The chop() function removes whitespace or other characters from the end of a string.

This function is an alias of rtrim() function.

The following table summarizes the technical details of this function.

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

Syntax

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

chop(string, charlist);

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

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

// Chopping whitespace from the end
echo chop($str);
?>

Parameters

The chop() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to be chopped.
charlist

Optional. Specifies which characters to remove from the string.

Without the charlist parameter, chop() will strip these 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 chop() function actually works:

In the following example only new line and tab characters from the end of the string will be chopped. Whitespace characters at the beginning and inside of the string will remain as is.

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

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

In the following example all exclamation mark at the end of the string will be removed.

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

// Chopping characters from the end
echo chop($str, "!");
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties