JustinK101 Posted November 11, 2008 Share Posted November 11, 2008 I have a string like: '$2,343.23' I need to make it a standard double, so basically remove $ and , and . Is there an easy way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/ Share on other sites More sharing options...
darkfreaks Posted November 11, 2008 Share Posted November 11, 2008 number_format money_format Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687343 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 str_replace(mixed $search , mixed $replace , mixed $subject); ?? <?php $dollar = "$2,343.23"; $dollar = str_replace("$","",$dollar); $dollar = str_replace(",","",$dollar); $dollar = str_replace(".","",$dollar); echo $dollar;//should print 234323 ?> I think you can do this as well <?php $dollar = "$2,343.23"; $replace = array("$",".",","); $dollar = str_replace($replace,"",$dollar); echo $dollar;//should print 234323 ?> Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687344 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 Using regex you can, since I am not good at regex here is an alternative method. <?php $number = '$2,343.23'; $number = str_replace('$', '', $number); $number = str_replace('.', '', $number); $number = str_replace(',', '', $number); echo $number; ?> Not the best, but yea without learning regex it works =). Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687345 Share on other sites More sharing options...
.josh Posted November 11, 2008 Share Posted November 11, 2008 probably a better way, but here's my take: <?php $x = "$2,343.23"; sscanf($x,"$%d,%d.%d",$num[0],$num[1],$num[2]); $y = implode('',$num); echo $y; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687348 Share on other sites More sharing options...
JustinK101 Posted November 11, 2008 Author Share Posted November 11, 2008 Thanks guys. Worked like a champ. Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687349 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 Not competitive or anything but who's did you use? Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687350 Share on other sites More sharing options...
.josh Posted November 11, 2008 Share Posted November 11, 2008 another: <?php $x = "$1,345.45"; $x = preg_replace("/[$,.]/","",$x); echo $x; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132229-convert-dollar-string-to-double/#findComment-687352 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.