PHP chop() Function
In this chapter you will learn:
- Definition for PHP chop() Function
- Syntax for PHP chop() Function
- Parameter for PHP chop() Function
- Return for PHP chop() Function
- Example - Remove characters from the right end of a string
- Example - Remove newlines (\n) from the right end of a string
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/* ja v a 2 s . co m*/
$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/*from ja va 2s . c o m*/
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP chr() Function
- Syntax for PHP chr() Function
- Parameter for PHP chr() Function
- Return for PHP chr() Function
- Note for PHP chr() Function
- Example - Convert number to character
Home » PHP Tutorial » PHP String Functions