What's the easiest way to remove a comma from the END of a list?

Discussion in 'PHP' started by x0x, Dec 20, 2009.

  1. #1
    I'm sure you guys have dealt with this before when generating a list using a loop.

    Let's say I have:

    $var = "one, two, three, four, five, ";

    and

    $var2 = "one , ";

    What would be the easiest way to remove the commas only from the end?
    So that:

    $var = "one, two, three, four, five ";

    and

    $var2 = "one ";
     
    Last edited: Dec 20, 2009
    x0x, Dec 20, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $var = preg_replace('/,\s*$/', '', $var);
    PHP:
    Regular expression would be my choice
     
    JAY6390, Dec 20, 2009 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also, if you want to keep the space there use
    $var = preg_replace('/,(\s*)$/', '$1', $var);
    PHP:
     
    JAY6390, Dec 20, 2009 IP
  4. minervait

    minervait Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can use substr function
    $var = substr($var,0,-1);
     
    minervait, Dec 21, 2009 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Thanks guys!
     
    x0x, Dec 21, 2009 IP
  6. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?php
    /**************************
    * Remove Ending Comma
    ***************************
    * Written by Joel Larson (Coded Caffeine)
    *   http://thejoellarson.com/
    * Date: 23.12.09
    *
    * Removes the comma at the end of a string, if present.
    **************************/
    
    # Sample String.
    $string = 'This is a test string with a comma,';
    
    /**
    * remove_end_comma()
    * PARAM: $input (The string to use)
    * RETURNS: -Nothing-
    *
    * Takes the $input variable (in this case, $string) and overwrites it.
    */
    function remove_end_comma(&$input)
    {
    	# Set the total length of the input.
    	$length = strlen(trim($input));
    	
    	# If the total length - 1 has a comma, then...
    	if( strpos($input, ',', --$length) !== FALSE )
    		# Remove the comma and overwrite the input.
    		$input = substr($input, 0, -1);
    }
    
    # Call the function.
    remove_end_comma($string);
    
    # $string no longer has a comma at the end.
    echo $string;
    PHP:
    If you want more explanations, feel free to ask. :)
     
    CodedCaffeine, Dec 23, 2009 IP
  7. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    
    rtrim($string, ', '); // removes both commas and spaces from the end of the string
    
    PHP:
     
    Gray Fox, Dec 23, 2009 IP
  8. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    To say "That's enthusiasm!", would be a gross understatement. :rolleyes:

    Tings dat make ya go, "Hmmm."
     
    szalinski, Jan 1, 2010 IP
  9. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Well the idea of the function I wrote is:

    Check if the comma is there
    If it is, remove it.
    If it's not, leave it alone.

    I tend to over-think, but it's not a bad thing. :p
     
    CodedCaffeine, Jan 1, 2010 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    
    print_r(explode(', ', $var));
    
    PHP:
     
    Kaizoku, Jan 1, 2010 IP