note 31637 modified in function.chop by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:54:30 +0000 |
| Subject: | note 31637 modified in function.chop by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79945@lists.php.net to get a copy of this message | ||
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;
}
?>
--was--
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]
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;
}
http://php.net/manual/en/function.chop.php