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? 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 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 ?> 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 =). 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; ?> 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. 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? 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; ?> 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
Archived
This topic is now archived and is closed to further replies.