PHP chop() Function

❮ PHP String Reference

Example

Remove characters from the right end of a string:

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

Definition and Usage

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


Syntax

chop(string,charlist)

Parameter Values

Parameter Description
string Required. Specifies the string to check
charlist Optional. Specifies which characters to remove from the string.
The following characters are removed if the charlist parameter is empty:
  • "\0" - NULL
  • "\t" - tab
  • "\n" - new line
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - ordinary white space


Technical Details

Return Value: Returns the modified string
PHP Version: 4+
Changelog: The charlist parameter was added in PHP 4.1.0

More Examples

Example

Remove newlines (\n) from the right end of a string:

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

The HTML output of the code above will be (View Source):

<!DOCTYPE html>
<html>

<body>

Hello World!

Hello World!

</body>
</html>

The browser output of the code above will be:

Hello World! Hello World!
Try it Yourself »

❮ PHP String Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.