PHP: Strip Leading Zeroes from a String Tips, Tricks and Tutorials 18 FEB 2014

green php elephants - elephpantsBecause ordering numbers is a whole lot easier when they all of the same length in terms of characters, with leading zeroes used for padding, the issue of stripping these unnecessary characters out for display purposes tends to crop up quite a lot.

Which is silly because PHP has a handy standard function that will do this for us with minimum effort.

The ltrim function strips whitespace from the beginning of a string. In addition, it has the option of stripping specified characters by means of the character_mask parameter.

Taking this into account, it then becomes trivial to strip leading zeroes from a string:

echo ltrim('0000005678','0');
//results in 5678

Related Link:

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.