-2

I'm trying to explode this part of a json string from Google's currency calc:

"3 670.758 U.S. dollars"

I would like the value and the currency text separate, i was originally using a white-space as the separator, however i noticed when i tried to convert a 4 figure number Google was adding a space between the first and rest of the digits, to separate thousands from hundreds etc.

Any thoughts on how i should tackle this one?

I'm wondering if there is a way to explode with a white-space still but skip the first one?

Thanks in advance

Danny
  • 242
  • 2
  • 12
  • 2
    Why do you need to deal with these kinds of values in the first place? Is there no way to get this data in a proper format? – Pekka Feb 15 '12 at 15:42
  • 1
    1) split at the second whitespace, then explode the second part. 2) explode, then merge the first two parts. – Karoly Horvath Feb 15 '12 at 15:43
  • 2
    @Pekka not very helpful. – Danny Feb 15 '12 at 15:46
  • Probably a regex would be better than just splitting based on spaces. – iDifferent Feb 15 '12 at 15:48
  • 3
    So thinking about whether there might be an API that returns the conversion results in a truly machine readable format (like JSON or XML), which would make it unnecessary to fiddle with regexes, be immune against future format changes, and (unlike this) comply with Google's [terms of service](http://www.google.com/accounts/TOS?hl=en&loc=us), is "not very helpful"? Well, suit yourself then... – Pekka Feb 15 '12 at 15:52
  • @Pekka re-read the first sentence of my question, that is part of the returned json string, i just posted the relevant part. Thanks for clarifying what you were getting at though :-) – Danny Feb 15 '12 at 18:15

4 Answers4

3

Try regular expressions

preg_match('#([0-9\s\.]+)(.+)#', '3 670.758 U.S. dollars', $result);

$result will be:

Array
(
    [0] => 3 670.758 U.S. dollars
    [1] => 3 670.758 
    [2] => U.S. dollars
)
alex347
  • 621
  • 7
  • 18
2

Don't do this.

Use a proper API that returns JSON or XML values instead. This will

  • make it unnecessary to fiddle with regexes

  • be immune against future format changes, which may occur any time in Google's calculator

  • actually comply with Google's terms of service, which your current approach doesn't:

    5.3 [...] You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.

See e.g. this answer for API suggestions. The Google API is deprecated, but the Yahoo one seems to be still working.

More possibly helpful resources:

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

I'm wondering a bit about what kind of formatting this is:

3 670.758 U.S. dollars
 |   `-- digit separator
 `-- thousand separator

Which would be ISO something, however I'm wondering about the three digits which aren't very common for prices. Probably you convert fractions of cents here because of money conversion. Just noting, so the context is clear.

To parse these kind of formatting, you can use a regular expression that specifies those, e.g. space as thousand separator, thousands are optional, . as digit separator, three digits. Currency name is separated by another space from the number, the whole string needs to be matched:

^(\d{1,3}(?: \d{3})*\.\d{3}) (.*)$

This expression can be used in PHP then with the preg_match function:

$str = '3 670.758 U.S. dollars';

$r = preg_match('~^(\d{1,3}(?: \d{3})*\.\d{3}) (.*)$~', $str, $matches);

if ($r) list(, $value, $currency) = $matches;


$value :    string(9)  "3 670.758"
$currency : string(12) "U.S. dollars"

The more correct you formulate the regular expression, the better results you'll get. Demo

hakre
  • 193,403
  • 52
  • 435
  • 836
0

Another variant, you can use explode 2 times, like this:

$str = "3 670.758 U.S. dollars";
$exp1 = explode(' ', $str, 2);
$exp2 = explode(' ', $exp1[1]);
$final = array($exp1[0].' '.$exp2[0], $exp2[1], $exp2[2]);

var_export($final);       // array ( 0 => '3 670.758', 1 => 'U.S.', 2 => 'dollars', )
CoursesWeb
  • 4,179
  • 3
  • 21
  • 27