I found out this little neat function today while searching for some currency converter or should I say currency rate grabber. What this function does is it performs a google search and since google’s calculator handles currency conversions pretty well and up to date in the rates it grabs the information from it with the use of a regular expression. To be honest I would’ve never though of this but when you think about it it’s more than obvious. Google provides a service that we are using when we could be abusing it!
Here’s the code, Enjoy!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function exchangeRate( $amount, $currency, $exchangeIn ) { $googleQuery = $amount . ' ' . $currency . ' in ' . $exchangeIn; $googleQuery = urlEncode( $googleQuery ); $askGoogle = file_get_contents('http://.google.com/search?q='.$googleQuery); $askGoogle = strip_tags( $askGoogle ); $matches = array(); if (preg_match("/Rates provided for information only - see disclaimer./i", $askGoogle)) { $matches = array(); preg_match( '/= (([0-9]|.|,| )*)/', $askGoogle, $matches ); return $matches[1] ? $matches[1] : false; } else { $status = 'google says no'; return $status; } } echo exchangeRate( 1000, 'euro', 'dollars' ); |
Aside from the currency converter this gave me the idea of a word translator using google and php which can be found here, or perhaps definition of words with google define. The possibilities are cute impressive when you put your mind to it.
No posts at the moment. Check back again later!