chopDescriptionchop() 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. ExampleExample 1190. Remove trailing whitespace with chop() <?php $string = "\t\tReckon or ask bird?\t\t"; echo "Original string: '$string'\n", "chop()'d string: '", chop($string), "'"; ?> Output: Original string: ' Reckon or ask bird ' chop()'d string: ' Reckon or ask bird' Example 1191. Simulate Perl's chop() function <?php // Placing & in front of the argument makes it passed by reference // This lets us simulate Perl's chop() - we operate directly on the variable, // removing the last character from the variable and returning it. function perl_chop(&$string) { $last_char = substr($string, -1); $string = substr($string, 0, -1); return $last_char; } $word = 'food'; echo "$word\n"; // Call perl_chop() and display the character removed from the string echo perl_chop($word), "\n"; // echo out the shortened version of the string echo $word, "\n"; ?> Output: food d foo
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|