PHP chop()Functions
Examples
Remove characters from the right side of a string:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>
Run Example »
Definition and Usage
The chop() function removes whitespace or other predefined characters from the right side of a string.
Syntax
chop(string,charlist)
| Parameters | Description |
|---|---|
| string | Required. Specifies the string to check. |
| charlist | Optional. Specifies which characters to remove from the string. If the charlist parameter is empty, the following characters are removed:
|
Technical Details
| Return Value: | Returns the modified string. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 4.1.0, thecharlistparameter was added. |
More Examples
Example 1
Remove the newline character (\n) from the right side of a string:
<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
The HTML output of the above code is as follows (view source):
<!DOCTYPE html>
<html>
<body>
Hello World!
Hello World!
</body>
</html>
<html>
<body>
Hello World!
Hello World!
</body>
</html>
The browser output of the above code is as follows:
Hello World! Hello World!
Run Example »
PHP String Reference Manual Other Extensions