This is a discussion on Perl style chomp() within the PHP General forums, part of the PHP Programming Forums category; I noticed, while perusing the chop() manual page, that some people were giving examples of Perl's chop() and chomp() ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
On 11/1/07, Jim Lucas <lists@cmsws.com> wrote:
> 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. what about trim(), rtrim() and ltrim() ? seems unnecessary to reinvent the wheel. if you want an array one, just make it recurse like you did. - mike |
|
|||
|
mike wrote:
> On 11/1/07, Jim Lucas <lists@cmsws.com> wrote: >> 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. > > what about trim(), rtrim() and ltrim() ? seems unnecessary to reinvent > the wheel. if you want an array one, just make it recurse like you > did. > > - mike > the functions that you mentioned do not do the same thing as what this function does. it alters a reference to the variable that you passed it and returns what it removed. The above listed functions return the altered string. Just a little difference. But, if you are comfortable using the Perl's chomp() function, then I think I created the PHP equivalent. -- 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 |