PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




chop

Name

chop — Strips trailing whitespace from a string.

Synopsis

string chop(string);
string string: String from which to remove trailing whitespace

Returns

String stripped of trailing whitespace

Description

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.

Note

Perl coders may be misled by this function's name. Perl's chop() function removes the last character from a string, while PHP's chop() behaves more like Perl's chomp() . To simulate the behavior of Perl's chop() , see the examples.

Version

PHP 3+, PHP 4+

See also

To strip whitespace from the start and/or end of a string:

ltrim()

rtrim()

trim()

To grab a substring from a string:

substr()



Example

Example 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.