chop – PHP String Functions
Syntax :
string chop (string, charlist);
Description :
chop function will remove predefined characters or specified characters in charlist if any from the right end of a string. This function is an alias of- rtrim() function.
Parameter :
string – String to check.
charlist – Optional. This is optional parameter, If any value is provided, then specified characters will be removed from right of a string.
The following characters are removed if the charlist parameter is empty:
- “\0” – NULL
- “\t” – tab
- “\n” – new line
- “\r” – carriage return
- ” ” – ordinary white space
- “\x0B” – vertical tab
Output :
This will return the string after removing specified characters in the charlist or predefined characters from right of a string.
Related functions : rtrim().
chop – PHP Functions Example 1 :
$strExample = chop("Hi from tutorialmines.net! ");
echo($strExample);
In above example ,We have string of “Hi from tutorialmines.net! “. So, when we apply chop function. This will remove all ” ” (ordinary white space) from right of a input string.
Now Output will become –
Hi from tutorialmines.net!
chop() with charlist() – PHP Functions Example 2 :
If you want to remove string ” chop – PHP String Functions” from the right hand side of a string. See below example for doing it.
$strExample = ("Hi from tutorialmines.net! chop - PHP String Functions");
echo $strExample . "<br>";
echo chop($strExample,"chop - PHP String Functions");
In above example ,We have string of “Hi from tutorialmines.net! chop – PHP String Functions”. So, when we apply chop function. This will remove string “chop – PHP String Functions” from right of a input string.
Output will be like below:
Hi from tutorialmines.net! chop - PHP String Functions
Hi from tutorialmines.net!