note 31637 deleted from function.chop by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 13:09:17 +0000 |
| Subject: | note 31637 deleted from function.chop by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155671@lists.php.net to get a copy of this message | ||
Note Submitter: thebitman at attbi dot com
----
Actually, PHP's chop() acts just as perl's chomp(). The above user-contributed example
does not act enough like perl's chop(), however. An important part of perl's chop() is
that it returns the chop()ed character. Here's how to get perl-style chop()s [let's call
it chomp() just to bring this whole mess full-circle]
<?php
function chomp(&$string)
{
if (is_array($string))
{
foreach($string as $i => $val)
{
$endchar = chomp($string[$i]);
}
} else {
$endchar = substr("$string", strlen("$string") - 1, 1);
$string = substr("$string", 0, -1);
}
return $endchar;
}
?>