Home »
PHP
PHP chop() Function with Example
PHP chop() Function: Here, we are going to learn about the chop() function with example in PHP.
Submitted by IncludeHelp, on February 07, 2019
PHP chop() Function
chop() function is a string function in PHP, it is used to remove the whitespaces or/and specified character/string from the end of the string.
Syntax:
chop(string, [char/string]) : string
Here,
- string is the main string.
- char/string is an optional parameter, if specifies the single character or set of characters to be removed from the end of the string. If we do not specify the char/string it removes the spaces.
Examples:
Input:
str = "IncludeHelp.com#"
char/string to remove "#"
Output:
"IncludeHelp.com"
Input:
str = "IncludeHelp.com#"
char/string to remove "com#"
Output:
"IncludeHelp."
Input:
str = "IncludeHelp.com#"
char/string to remove ".com#"
Output:
"IncludeHelp"
PHP code:
<?php
$str = "IncludeHelp.com#";
//removing "#"
echo (chop($str, "#")."\n");
//removing "com#"
echo (chop($str, "com#")."\n");
//removing ".com#"
echo (chop($str, ".com#")."\n");
?>
Output
IncludeHelp.com
IncludeHelp.
IncludeHelp
TOP Interview Coding Problems/Challenges