PHP chop() Function
Definition
The chop() function removes whitespaces or other predefined characters from the right end of a string.
Syntax
PHP chop() function has the following syntax.
chop(string,charlist)
Parameter
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to check |
charlist | Optional. | 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
Return
PHP chop() function returns the modified string
Example
Remove characters from the right end of a string:
<?php/*w ww .j a va 2 s.c om*/
$str = "Hello World!";
echo $str . "\n";
echo chop($str,"World!");
?>
The code above generates the following result.
Example 2
Remove newlines (\n) from the right end of a string:
<?php/*w w w . j a va 2 s .c o m*/
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
The code above generates the following result.