The chop() function removes whitespaces or other predefined characters from the right end of a string.
PHP chop() function has the following syntax.
chop(string,charlist)
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:
PHP chop() function returns the modified string
Remove characters from the right end of a string:
<?php
$str = "Hello World!";
echo $str . "\n";
echo chop($str,"World!");
?>
The code above generates the following result.
Remove newlines (\n) from the right end of a string:
<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
The code above generates the following result.