Bluehost.com Web Hosting $6.95

Perl style chomp()

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() ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-01-2007
Jim Lucas
 
Posts: n/a
Default Perl style chomp()

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
Reply With Quote
  #2 (permalink)  
Old 11-01-2007
mike
 
Posts: n/a
Default Re: [PHP] Perl style chomp()

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
Reply With Quote
  #3 (permalink)  
Old 11-01-2007
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Perl style chomp()

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:06 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0