In this article, you will learn;
- How to remove specific string/substring,
- Remove whitespaces from the string.
- Remove characters from the string.
The PHP CHOP() function solves this problem and provides the following functionality.
- Remove specified string, character or whitespaces from the string.
- Removal takes place from the right side of the given string.
What is the syntax of the CHOP() function in PHP?
chop(string,charlist)
Parameters | Details |
---|---|
string | The string to remove from – Required |
charlist | Optional. Specifies which characters to remove from the string. The following characters are removed if the charlist parameter is empty: – “\0” – NULL – “\t” – tab – “\n” – enter/new line – “\x0B” – verticle tab space – “\r” – carriage return – ” ” – whitespace |
Example of the CHOP() function
Example 1. In this example, we simply remove the specified characters from the given string.
<?php
$str = "Hello PHP!";
echo $str . "<br>";
echo chop($str,"PHP!");
?>
Example 2. In this example, we removed the new line /n from the given string.
<?php
$str = "Hello PHP!\n\n";
echo $str;
echo chop($str);
?>