Removing last character of a string

June 30, 2004 by  
Filed under Code Snippets

I had a string that looked like:

2;4;6;7;

The string is dynamic, so it could also be:

3;5;2;8;9;

I needed to remove the last ; (semicolon) from the string but wasn’t sure what the best method of doing this was. I thought about using the strlen() (string length) function. Get the value of how long the string is, and then removed the last character using substr(). The task seems a bit tedious and unefficient though.

So I searched Google to find the best method, and stumbled upon the answer to my question — How do you remove the last character of a string?

The solution is to use:
$outgoing = substr_replace($incoming,"",-1);

So if I use:
$outgoing = substr_replace('3;5;2;8;9;',"",-1);

My output would be:
3;5;2;8;9

Exactly what I was looking for! Thank you Graham Ellis!

Related Products:

Fire 7 Kids Edition Tablet, 7" Display, 16 GB, Blue Kid-Proof CaseFire 7 Kids Edition Tablet, 7" Display, 16 GB, Blue Kid-Proof Case

Powerful, full-featured Fire 7 tablet--with 1 year of Amazon FreeTime Unlimited and a Kid-Proof Case--for a savings of up to $129--plus a 2-year w... Read More >

SanDisk 64GB Ultra SDXC UHS-I Memory Card SDSDUNC-064G-GN6INSanDisk 64GB Ultra SDXC UHS-I Memory Card SDSDUNC-064G-GN6INTwice as fast as ordinary SDHC and SDXC cards to take better pictures and Full HD videos with your compact to mid-range point and shoot cameras and ca... Read More >
Tablet Stand Adjustable, Lamicall iPad Stand : Desktop Stand Holder Dock for new iPad 2017 Pro 9.7, 10.5, Air mini 2 3 4, Kindle, Nexus, Accessories, Tab, E-reader, other Tablets (4-13 inch) - SilverTablet Stand Adjustable, Lamicall iPad Stand : Desktop Stand Holder Dock for new iPad 2017 Pro 9.7, 10.5, Air mini 2 3 4, Kindle, Nexus, Accessories, Tab, E-reader, other Tablets (4-13 inch) - SilverLamicall iPad Pro Tablet Holder Stand
Al-Ti Alloy portable multi-angle adjustable holder stand for you lovely device when you are watching videos,... Read More >
Related Posts Plugin for WordPress, Blogger...
  • http://www.rattledbaby.com/ Bradley Batt

    substr() is probably the easiest, but you don’t have to use strlen(). Straight from the PHP manual:

    $rest = substr("abcdef", 0, -1); // returns "abcde"

  • http://www.rattledbaby.com/ Bradley Batt

    substr() is probably the easiest, but you don’t have to use strlen(). Straight from the PHP manual:

    $rest = substr("abcdef", 0, -1); // returns "abcde"

  • http://rebusmedia.com Sam

    This function will take the target string and string to look for and remove as an argument.

    //Trim string and remove specified trailing string;
    function removeCharacter($whichData,$whichString) {
    $whichData = trim($whichData);
    if(substr($whichData,strlen($whichData)-1) == $whichString) {
    $whichData = substr($whichData,0,strlen($whichData)-1);

    }
    return $whichData;
    };

  • http://rebusmedia.com Sam

    This function will take the target string and string to look for and remove as an argument.

    //Trim string and remove specified trailing string;
    function removeCharacter($whichData,$whichString) {
    $whichData = trim($whichData);
    if(substr($whichData,strlen($whichData)-1) == $whichString) {
    $whichData = substr($whichData,0,strlen($whichData)-1);

    }
    return $whichData;
    };

  • http://www.php.kz Sapenov

    rtrim($var,”;”)

    hope this helps

  • http://www.php.kz Sapenov

    rtrim($var,”;”)

    hope this helps

  • Kevin

    Is there a way to take all but the last string?

  • http:/// Kevin

    Is there a way to take all but the last string?

  • http://daynah.net daynah

    Do you mean the last character?

    echo substr(‘abcdef’, -1, 1);

    This will return just the ‘f’.

  • http://daynah.net daynah

    Do you mean the last character?

    echo substr(‘abcdef’, -1, 1);

    This will return just the ‘f’.

  • http://daynah.net daynah

    That is another great piece of code. :) Thank you!

  • http://daynah.net daynah

    That is another great piece of code. :) Thank you!

  • Kevin

    Thanks!

  • http:/// Kevin

    Thanks!

  • http://daynah.net daynah

    You’re welcome. :)

  • http://daynah.net daynah

    You’re welcome. :)

  • Jen

    Thanks :)

  • Jen

    Thanks :)