johnsmith153 Posted August 9, 2012 Share Posted August 9, 2012 I want to get the price from a text string $string = "sssssssssss ggggggggg $44 test here"; So to return 44 for above. The only thing is the currency could change to another value (e.g. a pound sign (which this site won't let me post)). I can find if the value exists etc. but the difficulty is in getting the value next to the symbol (e.g. 44 next to $). Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 9, 2012 Share Posted August 9, 2012 Is the currency symbol the only thing that would be different? For example, could the value be a decimal such as $44.32? What about grouping, e.g. 1,234.56? Will the value always have a space before and after it? Can a space be between the currency symbol and the first digit, e.g. $ 12? All of these are very important considerations when developing the right solution. As for the currency symbol you would have to define a list of the currency symbols that would be used. But, I'm not going to take time to write anything without answers to the above. Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted August 9, 2012 Author Share Posted August 9, 2012 No, just simple. So grab everything after a defined value and ignore if spaces in between. Also grab the first occurence only function get_price ($search_value, $string) { } echo get_price ("$", "sssssssssss ggggggggg $44 . 99 $$$ dffg test here"); // returns 44 Quote Link to comment Share on other sites More sharing options...
maxudaskin Posted August 9, 2012 Share Posted August 9, 2012 <?php $currency = '$'; $string = 'This item costs $29.97. You may pay cash or credit.'; $find = '/\\' . $currency . '[0-9]*[.]?[0-9]{0,2}/'; preg_match($find, $string, $matches); echo 'Searching for "' . $find . '" in "' . $string . '" <br />'; print_r($matches); Quote Link to comment 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.