PHP chop()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Remove characters from the right side of a string:

<?php
$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:
  • "\0" - NULL
  • "\t" - tab
  • "\n" - newline
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - space

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

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

<!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 Example »


PHP String 参考手册PHP String Reference Manual Other Extensions