Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : String Functions : chop

chop

Alias of rtrim (PHP 4, PHP 5)


Related Examples ( Source code ) » chop






Code Examples / Notes » chop

bernard dot spam

To get a perl style chop():
<?php
$foo = "bar";
$foo = substr("$foo", 0, -1);
echo $foo;
//returns "ba" (removes last character)
?>


zebadger@excite dhot com

The perl version of chomp only removes newlines.  I modified the previous code to do that.
<?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);
               if ($endchar == "\n")
               {
                       $string = substr("$string", 0, -1);
               }
       }
       return $endchar;
}
?>


php

The above notes are confusing 2 different Perl fuctions, chop and chomp.
In Perl, chop removes and returns the last charecter of a string.  This function was often used in Perl <= version 4 to remove newlines, and in some cases, you'd think you were choping a new line, but if the last charecter wan't a newline, chop would still remove it.
As of Perl 5, chomp (note the "m") was introducted.  Chomp removes the last character(s) of a string only if those chatecters are new lines, and it returns the number of characters deleted.  (If the string ends in multiple newlines, they will all be choped off, and if the last charecter is not a new line, nothing will be choped off.)
So, I don't think either of the above examples quite duplicate Perl's chomp, but they are both very helpful.


thebitman

Just doing an ego search, noticed this old thread. I think all of you have missed my point- I was trying to emulate perl's chop() function, *NOT* perl's chomp() function. I just called it chomp() in PHP as chop() is already taken.
Perl's chop() does two very important things (in my arrogant opinion):
-If given an array, it will chop() every element in the array
-It returns the character removed (leading to some while loops which certain people would murder you for using, and certain other people would go out of their way to use)
I guess calling it chomp() was a bad idea.. pretend I said "perl_chop()" instead.


sam

I don't know Perl so I am not sure how close/far this is from its chop() behavior but if you just want to remove line endings from a string you can do...
<?php
rtrim($string,"\n\r");
?>
All of PHP's trim function accept an optional character mask.


anon

Another possible one would be to use this:
<?php
function chup(){
$ar=Array();
foreach(func_get_args() as $b) {
push($ar,$b[strlen($b)-1]);
&$b[strlen($b)-1]='';
 }
return $ar;
}
?>
If you wanted to perl-chop a va list of strings and return the removed chars. Obviously you can easily mod it for va list arrays of strings and the like.


thebitman

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;
}
?>


Change Language


Follow Navioo On Twitter
addcslashes
addslashes
bin2hex
chop
chr
chunk_split
convert_cyr_string
convert_uudecode
convert_uuencode
count_chars
crc32
crypt
echo
explode
fprintf
get_html_translation_table
hebrev
hebrevc
html_entity_decode
htmlentities
htmlspecialchars_decode
htmlspecialchars
implode
join
levenshtein
localeconv
ltrim
md5_file
md5
metaphone
money_format
nl_langinfo
nl2br
number_format
ord
parse_str
print
printf
quoted_printable_decode
quotemeta
rtrim
setlocale
sha1_file
sha1
similar_text
soundex
sprintf
sscanf
str_getcsv
str_ireplace
str_pad
str_repeat
str_replace
str_rot13
str_shuffle
str_split
str_word_count
strcasecmp
strchr
strcmp
strcoll
strcspn
strip_tags
stripcslashes
stripos
stripslashes
stristr
strlen
strnatcasecmp
strnatcmp
strncasecmp
strncmp
strpbrk
strpos
strrchr
strrev
strripos
strrpos
strspn
strstr
strtok
strtolower
strtoupper
strtr
substr_compare
substr_count
substr_replace
substr
trim
ucfirst
ucwords
vfprintf
vprintf
vsprintf
wordwrap
eXTReMe Tracker