Removing last character of a string
June 30, 2004 by daynah
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:
Electronics and Circuit Analysis Study Guide: Signal Transforms, Fourier, Laplace & Z transform, Transfer function, Electronic components, Analog & Digital Circuits (Mobi Study Guides)Boost Your grades with this illustrated study guide! You will use it from an undergraduate school all the way to graduate school and beyond.
How to Diagnose and Fix Everything ElectronicMaster the Art of Electronics Repair In this hands-on guide, a lifelong electronics repair guru shares his tested techniques and invaluable ... Read More >
Kindle Fire, Full Color 7" Multi-touch Display, Wi-FiMovies, apps, games, music, reading and more, plus Amazon's revolutionary cloud-accelerated web browser - 18 million movies, TV shows, songs, magazine... Read More >
Popularity: 23% [?]


@







