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 $). Link to comment https://forums.phpfreaks.com/topic/266882-get-value-from-text-string/ 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. Link to comment https://forums.phpfreaks.com/topic/266882-get-value-from-text-string/#findComment-1368218 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 Link to comment https://forums.phpfreaks.com/topic/266882-get-value-from-text-string/#findComment-1368220 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); Link to comment https://forums.phpfreaks.com/topic/266882-get-value-from-text-string/#findComment-1368225 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.