king.oslo Posted June 6, 2009 Share Posted June 6, 2009 Hey everyone, I am looking to use preg_replace() twice. Can you please tell me how to: - replace all characters in a $sting that are not numbers ',' and '.' with ''. - replace all ',' in $string with '.' Thanks! Marius Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/ Share on other sites More sharing options...
.josh Posted June 6, 2009 Share Posted June 6, 2009 $string = preg_replace('~[^0-9,.]~','',$string); $string = str_replace(',','.',$string); Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/#findComment-850387 Share on other sites More sharing options...
Ken2k7 Posted June 6, 2009 Share Posted June 6, 2009 What good will the second one do? Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/#findComment-850389 Share on other sites More sharing options...
.josh Posted June 6, 2009 Share Posted June 6, 2009 $string = "abc123,456.78"; $string = preg_replace('~[^0-9,.]~','',$string); // new value: 123,456.78 $string = str_replace(',','.',$string); // new value: 123.456.78 Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/#findComment-850392 Share on other sites More sharing options...
Ken2k7 Posted June 6, 2009 Share Posted June 6, 2009 Oh I overlooked it. Sorry about that Crayon Violet. Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/#findComment-850395 Share on other sites More sharing options...
king.oslo Posted June 6, 2009 Author Share Posted June 6, 2009 Crayon Violent: Thank you!oz Quote Link to comment https://forums.phpfreaks.com/topic/161143-solved-preg_replace-replace-all-letters-apart-from-numbers-and/#findComment-850398 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.