PHP function chop() – Alias of rtrim()

The chop() function is the pseudonym of rtrim(). This function was introduced in PHP3. chop() removes all trailing whitespace characters from the given string. These characters include the horizontal tab (\t), linefeed (\n), vertical tab (\013), carriage return (\r), and space (‘ ”) characters.

Syntax:

string chop(string string)

Return Values

String stripped of trailing whitespace

Examples

Example 1: Remove trailing whitespace with chop().

<?php 
$string = "\t\tI am a Geek\t\t"; 
echo "Original string: '$string'\n", 
     "chop()'d string: '", chop ($string), "'"; 
?>

Output:

Original string: '        I am a Geek       ' 
chop()'d string: '        I am a Geek'

Example 2:

<?php
$text = “\t\t We are using chop :) ... “; $choped = chop($text);
echo $choped.”
”; $choped = chop($text,” \t.”); echo $choped.”
”; ?>

The output of the above program is as follows:

We are using chop :) ... We are using chop :)
Note: chop() is different than the Perl chop() function, which removes the last character in the string.
Related Post