Page 1 of 1

string formatting

Posted: Wed Feb 20, 2008 8:58 am
by dannyd
Hi,

If I have the following

00030
00005
02830
00002
00200

How do I remove the 0's preceding 0's so after formatted it looks like

30
5
2830
2
200


?????

Re: string formatting

Posted: Wed Feb 20, 2008 9:10 am
by dannyd
I thought of using ltrim but i dont think that works with characters. Is there a php function that will remove preceding 0's starting from the left ?

Re: string formatting

Posted: Wed Feb 20, 2008 10:33 am
by liljester
try the number_format() function.

Code: Select all

$number = "00300";
print number_format($number);

Re: string formatting

Posted: Wed Feb 20, 2008 3:22 pm
by pickle
PHP Manual wrote:ltrim — Strip whitespace (or other characters) from the beginning of a string
ltrim() is built to strip characters...so it'll work with characters.

Both ltrim() and number_format() would work, though without doing any testing, I'd guess ltrim() would be a few nanoseconds faster, though number_format() is probably the better choice, as you are formatting numbers.

You could also just add zero to all the numbers & print the result. I believe casting them to integers should also do the trick.