w3resource

PHP: chop() function

Description

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.

Version:

(PHP 4 and above)

Syntax:

chop(string_name, char_list)

Parameters:

Name Description Required /
Optional
Type
string_name The string that will be trimmed. Required String
char_list Specifies which character to remove from the string. Without this parameter, the function removes the following characters.
An ordinary space.
\0" - the NULL-byte.
\t" - tab.
\n" - line feed.
\x0B" - a vertical tab.
"\r" - a carriage return.
Optional String

Return values:

The changed string.

Value Type: String.

Pictorial Presentation

php-string-chop()

Example:

<?php
$text1="w3resource.com  ";
$text2="w3resource.com\t\t";
$text3="\t\tw3resource.com\x0A";
$text4="Good Morning";
$trimmed_text=chop($text1);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=chop($text2);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=chop($text3);
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=chop($text1,'w3');
var_dump($trimmed_text);
echo '<br>';
$trimmed_text=chop($text4,"ng");
var_dump($trimmed_text);
?>

Output:

string(14) "w3resource.com"
string(14) "w3resource.com"
string(16) "w3resource.com"
string(16) "w3resource.com"
string(10) "Good Morni" 

View the example in the browser

See also

PHP Function Reference

Previous: bin2hex
Next: chr



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Constants can be defined inside classes using a const keyword.

Example:

class Foo {
 const BAR_TYPE = "bar";
 // reference from inside the class using self::
 public function myMethod() {
 return self::BAR_TYPE;
 }
}
// reference from outside the class using <ClassName>::
echo Foo::BAR_TYPE;

Output:

bar

This is useful to store types of items.

<?php
class Logger {
 const LEVEL_INFO = 1;
 const LEVEL_WARNING = 2;
 const LEVEL_ERROR = 3;
 // we can even assign the constant as a default value
 public function log($message, $level = self::LEVEL_INFO) {
 echo "Message level " . $level . ": " . $message;
 }
}
$logger = new Logger();
$logger->log("Info"); // Using default value
$logger->log("Warning", $logger::LEVEL_WARNING); // Using var
$logger->log("Error", Logger::LEVEL_ERROR); // using class

Output:


 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook