JSHINER Posted September 9, 2008 Share Posted September 9, 2008 I have something that pulls from the database "001904" - but I need those leading zeros gone when echoed, but need them there when in the database. Obviously str_replace() won't work because it will get rid of zeros in between numbers. How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/123525-getting-rid-of-leading-zeros/ Share on other sites More sharing options...
lisa71283 Posted September 9, 2008 Share Posted September 9, 2008 Cast the result as an integer. Quote Link to comment https://forums.phpfreaks.com/topic/123525-getting-rid-of-leading-zeros/#findComment-637924 Share on other sites More sharing options...
sasa Posted September 9, 2008 Share Posted September 9, 2008 look <?php $a = '001020.300'; echo $a + 0; ?> Quote Link to comment https://forums.phpfreaks.com/topic/123525-getting-rid-of-leading-zeros/#findComment-637939 Share on other sites More sharing options...
gbunny Posted September 9, 2008 Share Posted September 9, 2008 If you don't want to cast it as an int you could use the ltrim function. It would look something like this. <? $newString = ltrim($yourString, "0"); ?> You can find the documentation here http://www.php.net/manual/en/function.ltrim.php Quote Link to comment https://forums.phpfreaks.com/topic/123525-getting-rid-of-leading-zeros/#findComment-637963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.