Jump to content

[SOLVED] string replace


jakebur01

Recommended Posts

I am trying to get just the number from the following. $25.600 (EA) or $12,452.58(EA) or $254.600 (EA) or $25.64EA

 

All I am wanting is the number without the characters or commas. Ex.  25.60 or 12452.58

 

Would it be something like this?

 $targetChars3=array('"', '$', ',','a','b','c','d',e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','(',')','&','#','%','^');
   $listprice=str_replace($targetChars3, "", $listprice);

 

Link to comment
https://forums.phpfreaks.com/topic/145497-solved-string-replace/
Share on other sites

try this

 

$price  = $price_here; //i.e. $12,452.58(EA)
$to_remove = array("$",".", ",", "(EA)");
$to_put_in   = array("","","","");

echo str_replace($to_remove,$to_put_in,$price) ;

 

basicaly replacing characters with "" or nothing...let me know how it goes....i tested it myself and it works

It worked great. I had to remove the decimal because I didn't want it to pull those out. My only concern is I do not know what all may come after the price. It may not always be (EA).

 

It will allways be this format though....... $ 000.000 (EA)orSomethingElse

If that's the only thing in your string, drisate's method is most efficient option.  Though he forgot to allow for dots and \d allows for exponents, so you can make that stricter by using 0-9 range instead:

 

// $string is the string holding the number
$string = preg_replace('~[^0-9.]~','',$string);

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.