What is chop function in php?

(PHP 4, PHP 5, PHP 7, PHP 8)

chopAlias of rtrim()

Description

This function is an alias of: rtrim().

Notes

Note:

chop() is different than the Perl chop() function, which removes the last character in the string.

Kubo2

7 years ago

Rather use rtrim(). Usage of chop() is not very clear nor consistent for people reading the code after you.

JumpIfBelow

7 years ago

If you are searching for a function that does the same trick as chop in PERL, then you should just do the following code:
   $str = substr($str, 0, -1);
?>

The question is: why isn't chop() an alias of the code above, rather than something which will trap developpers?

krkbpk at gmail dot com RamaKrishna Kothamasu

9 years ago

//simple example function for chop()
echo "

";//without 
 you cann't see desired output in your browser
echo chop("   Ramki   ");//right spaces are eliminated
echo chop("Ramkrishna", "a..z");
echo
"
"
;
?>
/*output
  ------
  RamkiR*/

rahuls8730 at gmail dot com

13 years ago

Definition and Usage

The chop() function will remove a white space or other predefined character from the right end of a string.

This function is an alias of the rtrim() function.
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 allowed and is set to be removed if the charlist parameter is empty:

    * "\0" - ASCII 0, NULL
    * "\t" - ASCII 9, a tab
    * "\n" - ASCII 10, a new line
    * "\x0B" - ASCII 11, a vertical tab.
    * "\r" - ASCII 13, a carriage return
    * " " - ASCII 32, an ordinary white space

anon at mailinator dot com

15 years ago

Another possible one would be to use this:

function chup(){$ar=Array();

foreach(

func_get_args() as $b) {push($ar,$b[strlen($b)-1]);

&

$b[strlen($b)-1]='';

  }

return

$ar;

}

?>

If you wanted to perl-chop a va list of strings and return the removed chars. Obviously you can easily mod it for va list arrays of strings and the like.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The chop() in PHP is used to remove white spaces or any other specified characters from the end of a string.

    Syntax:

    string chop($string, $character)
    

    Parameters: This function accepts two parameters as shown in the above syntax and are described below:

    1. $string : It is used to specify the string which is needed to be checked.
    2. $character : It specifies the character which is needed to be removed from the given string. If this parameter is not specified then NULL, tab, newline, vertical tab, carriage return and ordinary white space are removed automatically.

    Return Value: The return type of the chop() function is string. It returns the string after removing the specified characters from the end.

    Program 1: In the below program a string is initialized as hello geeks!, by using chop() function the characters – ‘s’ and ‘!’ are removed from the end of the string.

    $s= "Hello Geeks!";

    echo $s. "\n";

    echo chop($s, "s!");

    ?>

    Output:

    Hello Geeks!
    Hello Geek
    

    Program 2: In the below program, since no character parameter is mentioned. Then automatically the newlines will be removed from the given string.

    $s= "Hello Geeks! \n best wishes \n \n";

    echo $s;

    echo chop($s);

    echo $s;

    ?>

    Output:

    Hello Geeks! 
     best wishes 
     
    Hello Geeks! 
     best wishesHello Geeks! 
     best wishes 
     
    

    Reference:
    http://php.net/manual/en/function.chop.php

    How do I delete a word in PHP?

    PHP str_replace() Function.
    Replace the characters "world" in the string "Hello world!" with "Peter": ... .
    Using str_replace() with an array and a count variable: $arr = array("blue","red","green","yellow"); ... .
    Using str_replace() with fewer elements in replace than find: $find = array("Hello","world");.

    What is PHP Ltrim?

    Definition and Usage. The ltrim() function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim() - Removes whitespace or other predefined characters from the right side of a string.

    What is substring PHP?

    The substr() is a built-in function of PHP, which is used to extract a part of a string. The substr() function returns a part of a string specified by the start and length parameter. PHP 4 and above versions support this function.

    Which of the following is an alias for the chop () PHP function?

    The chop() function is used to remove the whitespaces and other predefined characters from the right side of a string. This function is an alias of rtrim() function.