jakebur01 Posted February 17, 2009 Share Posted February 17, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/ Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 Use preg_match and it can pull that data out. If there are multiples on the page preg_match_all Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763861 Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 <?php $num = 't54j6 7kert @ ( ) _ - { }3 | |\\/ !@#$5%^&*()_+='; $new_num = preg_replace ('/[^\d]/', '', $num); echo $new_num; ?> Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763865 Share on other sites More sharing options...
jeger003 Posted February 17, 2009 Share Posted February 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763870 Share on other sites More sharing options...
jakebur01 Posted February 17, 2009 Author Share Posted February 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763881 Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 bro i gave you the awnser already ... <?php $num = '$25.600 (EA)'; $new_num = preg_replace ('/[^\d]/', '', $num); echo $new_num; ?> now all you need is add the $ and format the number with . or , http://ca3.php.net/number_format Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763890 Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763892 Share on other sites More sharing options...
drisate Posted February 17, 2009 Share Posted February 17, 2009 or reformat them the way you like http://ca3.php.net/number_format hehe Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763894 Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 well in OP he said he just wanted numbers and dots. Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763899 Share on other sites More sharing options...
jakebur01 Posted February 17, 2009 Author Share Posted February 17, 2009 Thank you all for your help. It is working great now! Quote Link to comment https://forums.phpfreaks.com/topic/145497-solved-string-replace/#findComment-763902 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.