CBG Posted March 25, 2011 Share Posted March 25, 2011 First problem fixed. My second problem is if the result is 0X.XX,0 or 0X.XX,1 I would like to remove the first 0 The X.XX are numbers, but the 0 is not always there. Can anyone help please Thanks Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/ Share on other sites More sharing options...
CBG Posted March 25, 2011 Author Share Posted March 25, 2011 I have the first problem fixed. With it removing ,0 or ,1 by using $str2 = $newstring; $pattern2 = "{\,1||,0+}"; $newstring2 = preg_replace($pattern2,"",$str2); echo '<br>'; echo $newstring2; Now it just leaves removing the first 0 if it exists Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192188 Share on other sites More sharing options...
kenrbnsn Posted March 25, 2011 Share Posted March 25, 2011 This pattern will catch either ",0" or ",1": <?php $pattern2 = "{\,(0|1)+}"; ?> To trim off the zero at the beginning, you can use the ltrim() funtion: <?php $str = '0x.xx'; echo ltrim($str,'0'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192190 Share on other sites More sharing options...
CBG Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks for the reply. This is working to well $str = '0x.xx'; echo ltrim($str,'0'); Let say it is 00.00 I am wanting to remove the first 0 Then again it could be 01.00 I want to remove the first 0 Or it could be 10.00 which mean there is no 0 to remove The numbers could be anything. Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192194 Share on other sites More sharing options...
kenrbnsn Posted March 25, 2011 Share Posted March 25, 2011 You can use the function sprintf(): <?php $str = '00.00'; echo sprintf("%2.2f",$str); ?> Ken Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192201 Share on other sites More sharing options...
CBG Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks Ken Thanks got it. Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192206 Share on other sites More sharing options...
sasa Posted March 25, 2011 Share Posted March 25, 2011 try <?php $para = '010.00,1'; echo preg_replace('/0?(\d+\.\d+),(0|1)/', '\1', $para); ?> Link to comment https://forums.phpfreaks.com/topic/231694-preg_replace-remove-0-or-1-also-remove-first-zero/#findComment-1192245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.