X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

PHP chop() Function

PHP String Reference PHP String Reference

Example

Remove characters from the right end of a string:

<?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>

Run example »

Definition and Usage

The chop() function removes whitespaces or other predefined characters from the right end of a string.


Syntax

chop(string,charlist)

Parameter Description
string Required. Specifies the string to check
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" - new line
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - ordinary white space

Technical Details

Return Value: Returns the modified string
PHP Version: 4+
Changelog: The charlist parameter was added in PHP 4.1.0


More Examples

Example 1

Remove newlines (\n) from the right end of a string:

<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>

The HTML output of the code above will be (View Source):

<!DOCTYPE html>
<html>

<body>

Hello World!

Hello World!

</body>
</html>

The browser output of the code above will be:

Hello World! Hello World!

Run example »


PHP String Reference PHP String Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook