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 ? 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 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 ? 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. 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> 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 ?> 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); 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 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; Link to comment https://forums.phpfreaks.com/topic/92114-string-formatting/#findComment-471976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.