I noticed, while perusing the chop() manual page, that some people were giving examples of Perl's
chop() and chomp() functions. Comments made about both said the examples were good, but not correct.
A person gave an explanation of what the chomp() function did. So, I took it upon myself to try and
create a chomp() function using PHP. But, since I have absolutely no Perl knowledge, I was hoping
someone on the list would take a few seconds and check out my attempted creation and tell me if I'm
right or wrong.
Also, with regards to the chop() and chomp() functions, does Perl consider both "\r" and "\n" as
chars it would trim, or just "\n"?
TIA
<?php
/**
* chomp This is to mimic the functionality of Perl's chomp function
*
* @param mixed Input value, could be a string, could be an array()
* @return mixed If input is an array, it returns an array(). If
* input is a string, chomp() will return a string. If
* input is not either a string or array(), then chomp()
* returns false
*/
function chomp(&$input) {
# Test for input being a string
if ( is_string($input) ) {
# Return the difference in length after removing any trailing
# new lines or carriage returns
return (int)(strlen($input) - strlen(rtrim($input, "\r\n")));
}
# Test for input being an array()
if ( is_array($input) ) {
# Initialize return array()
$return_char = array();
# Loop through input array()
foreach($input as $i => $val) {
# Call chomp() recursively
$return_char[$i] = (int)chomp($input[$i]);
}
# Return compiled array() of rtrim()'ed lengths
return $return_char;
}
# Return false for anything else
return false;
}
$data[] = "something \n \n ";
$data[] = "something \n\n\n";
$data[] = "something \n\n";
var_dump(chomp($data));
$data = "something \n\n\n";
var_dump(chomp($data));
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php