PHP chop() Function

Example

Remove characters from the end of the string:

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

Run Instances

Definition and Usage

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

Syntax

chop(string,charlist)
Parameter Description
string Required. Specifies the string to be checked.
charlist

Optional. Specifies which characters to remove from the string.

If charlist If the parameter is empty, the following characters are removed:

  • "\0" - NULL
  • "\t" - Tab
  • "\n" - Newline
  • "\x0B" - Vertical Tab
  • "\r" - Carriage Return
  • " " - Space

Technical Details

Return Value: Returns the modified string.
PHP Version: 4+
Update Log: Added in PHP 4.1.0: charlist Parameter.

More Examples

Example 1

Remove the newline character at the end of the string (\n):

<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>

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

<!DOCTYPE html>
<html>
<body>
Hello World!
Hello World!
</body>
</html>

The browser output of the above code is as follows:

Hello World! Hello World!

Run Instances