Login   Register  
PHP Classes
elePHPant
Icontem

File: googlecc.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Brian Formalejo  >  Currency from google  >  googlecc.php  
File: googlecc.php
Role: Class source
Content type: text/plain
Description: Fetches the currency converter query string
Class: Currency from google
Convert money currency using Google search queries
 

Contents

Class file image Download
<?php

/*
    Google currency converter    
*/

class CC{

    
// The url to fetch search results, do not change #{money}# #{moneyfrom}# #{moneyto}#
    
var $url 'http://www.google.com.ph/search?q=#{money}#+#{moneyfrom}#+to+#{moneyto}#';
    
    function 
Convert$denomination$fromCurrency $toCurrency ){
        
$finalurl str_replace( array('#{money}#','#{moneyfrom}#','#{moneyto}#'), array( $denomination$fromCurrency$toCurrency),$this->url );
        
        
# Renders the google page result
        
$htmlrender file_get_contents$finalurl );        
        
preg_match_all('/\<h2 class\=r\>\<font size\=\+1\>\<b\>([0-9.]+[^a-zA-Z]+[0-9.]+)[^a-zA-Z]+([a-zA-Z\ ]+) = ([0-9.]+[^a-zA-Z]+[0-9.]+)[^a-zA-Z]+([a-zA-Z\ ]+)\<\/b\>\<\/h2\>/i',$htmlrender,$matches);
        
        
# Returns an array of results array(from_amount, from_currency, to_amount , to_currency)
        
return (!empty($matches[4][0])) ? array('from_amount'=>$matches[1][0],'from_currency'=>$matches[2][0],'to_amount'=>$matches[3][0],'to_currency'=>$matches[4][0]) : false;

    }

}

/*

    Example usage
*/
    
$cc = new CC();
    
$result $cc->Convert(3000'MXN''USD');
    
print_r($result);
/*
    This will return an array:
    Array
    (
        [from_amount] => 3000
        [from_currency] => Singapore dollars
        [to_amount] => 96 591.6846
        [to_currency] => Philippine pesos
    )    
    

*/


?>