THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP chop() Function

PHP String Reference PHP String Reference

Example

Remove characters from the right end of a string:

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

Definition and Usage

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


Syntax

chop(string,charlist)

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 1

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!
Run example »

PHP String Reference PHP String Reference