Jump to content

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.