String Functions

PHP chop() Function

Topic: PHP String ReferencePrev|Next


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+


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

chop(string, charlist);

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

// Sample string
$str = "  Hello World! ";

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


The chop() function accepts the following parameters.

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

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.

// 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.

// 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