dannyd Posted February 20, 2008 Share Posted February 20, 2008 Hi, If I have the following output 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 I thought ltrim would be the answer but I dont think it removes characters from the left such as removing zeros. Any ideas ? Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/ Share on other sites More sharing options...
Isityou Posted February 20, 2008 Share Posted February 20, 2008 Have you looked into regular expressions? Also str_replace() function might be useful. http://us.php.net/str_replace Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471712 Share on other sites More sharing options...
dannyd Posted February 20, 2008 Author Share Posted February 20, 2008 no i havent. How would a regular expression work to solve it ? Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471716 Share on other sites More sharing options...
Isityou Posted February 20, 2008 Share Posted February 20, 2008 You search and replace the zeros. Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471719 Share on other sites More sharing options...
effigy Posted February 20, 2008 Share Posted February 20, 2008 <pre> <?php $str = '00030'; echo ltrim($str, '0'); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471735 Share on other sites More sharing options...
thebadbad Posted February 20, 2008 Share Posted February 20, 2008 You can use intval, if your strings only contain numbers: <?php $val = 0032; $val = intval($val); // $val = 32 ?> Else ltrim as you suggested: <?php $val = '0032'; $val = ltrim($val, '0'); // $val = 32 ?> Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471736 Share on other sites More sharing options...
dannyd Posted February 20, 2008 Author Share Posted February 20, 2008 Thanks for the quick responses and help! I used the following below using the tip about regular expressions. $length = preg_replace("/^0*/",'', $val); Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471761 Share on other sites More sharing options...
kenrbnsn Posted February 20, 2008 Share Posted February 20, 2008 Why use preg_replace() for a simple task, when using sprintf() will work fine: <?php $strs = array('00030','00005','02830','00002','00200'); foreach ($strs as $str) echo $str . "<br>\n"; echo "<br>\n"; foreach ($strs as $str) echo sprintf("%d",$str) . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471769 Share on other sites More sharing options...
sasa Posted February 20, 2008 Share Posted February 20, 2008 echo "00023409" + 0; Quote Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471976 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.