PHP chop() Function

Example

Remove characters from the right 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 right end of the string.

Syntax

chop(string,charlist)
Parameters 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, remove the following characters:

  • "\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 Parameters.

More Examples

Example 1

Remove newline characters (\n) from the right side of the string:

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