PHP chop() Function
Topic: PHP String ReferencePrev|Next
Description
The chop()
function removes whitespace or other characters from the end of a string.
This function is an alias of rtrim()
function.
The following table summarizes the technical details of this function.
Return Value: | Returns the modified string. |
---|---|
Version: | PHP 4+ |
Syntax
The basic syntax of the chop()
function is given with:
The following example shows the chop()
function in action.
Example
Run this code »<?php
// Sample string
$str = " Hello World! ";
// Chopping whitespace from the end
echo chop($str);
?>
Parameters
The chop()
function accepts the following parameters.
Parameter | Description |
---|---|
string | Required. Specifies the string to be chopped. |
charlist |
Optional. Specifies which characters to remove from the string. Without the charlist parameter,
|
More Examples
Here're some more examples showing how chop()
function actually works:
In the following example only new line and tab characters from the end of the string will be chopped. Whitespace characters at the beginning and inside of the string will remain as is.
Example
Run this code »<?php
// Sample string
$str = "\tHello\n World!\n\t";
// Chopping the trailing whitespace
echo rtrim($str);
?>
In the following example all exclamation mark at the end of the string will be removed.
Example
Run this code »<?php
// Sample string
$str = "Hello World!!!";
// Chopping characters from the end
echo chop($str, "!");
?>