Jump to content

Convert Dollar String To Double


JustinK101

Recommended Posts

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
?>

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 =).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.