All pastes #388361 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-03-10 00:30 UTC
rendered paste body
	function parseGoogleFinance()
	{
		if ( $f = @fopen("http://finance.google.com/finance", "r") )
		{
			$html = "";
			
			$retval = array( "indices" => array() , "currency" => array() , "summary" => array() );
			$indices = array( "Dow" , "Nasdaq" , "S&P 500" );
			$currency = array( "USD-Euro" , "USD-Yen" , "USD-GBP" );
			
			while ( !feof($f) ) $html .= fread($f, 8192);
			
			fclose($f);
			
			$len = count($indices);
			for ($i = 0; $i < $len; $i++)
			{
				if ( preg_match("/<tr.*?>\s+<td.*?>.*?<a.*?>". $indices[$i] ."<\/a><\/td>\s+<td.*?><span.*?>(.*?)<\/span><\/td>\s+<td.*?>\s+<span.*?>(.*?)<\/span>\s+<\/td>\s+<td.*?>\s+<span.*?>(.*?)<\/span><\/td>\s+<\/tr>/i", $html, $reg) )
				{
					$retval["indices"][] = array(
						"index" => $indices[$i] ,
						"quote" => $reg[1] ,
						"points" => $reg[2] ,
						"percent" => $reg[3]
					);
				}
			}
			
			// 10y bond
			if ( preg_match("/<tr>\s+<td.*?>10y bond<\/td>\s+<td>(.*?)<\/td>\s+<td><span.*?>(.*?)<\/span><\/td>\s+<td><span.*?>(.*?)<\/span><\/td>\s+<\/tr>/i", $html, $reg) )
			{
				$retval["currency"][] = array(
					"name" => "10y bond" ,
					"rate" => $reg[1] ,
					"increase" => $reg[2] ,
					"percent" => $reg[3]
				);
			}
			
			// currency exchange
			$len = count($currency);
			for ($i = 0; $i < $len; $i++)
			{
				if ( preg_match("/<tr>\s+<td.*?><a.*?>". $currency[$i] ."<\/a><\/td>\s+<td>(.*?)<\/td>\s+<td><span.*?>(.*?)<\/span><\/td>\s+<td><span.*?>(.*?)<\/span><\/td>\s+<\/tr>/i", $html, $reg) )
				{
					$retval["currency"][] = array(
						"name" => $currency[$i] ,
						"rate" => $reg[1] ,
						"increase" => $reg[2] ,
						"percent" => $reg[3]
					);
				}
			}
			
			// sector summary
			if( $reg = preg_split("/<div.*?>&nbsp;<h3>Sector Summary<\/h3>.*?<\/tr>/is", $html) )
			{
				if ( $reg = preg_split("/<h3>Top&nbsp;movers<\/h3>/im", $reg[1]) )
				{
					if ( preg_match_all("/<td.*?><a.*?>(.*?)<\/a><\/td>.*?<span.*?>(.*?%)<\/span>\s+<\/td>.*?<\/table>/is", $reg[0], $reg) )
					{
						$len = count($reg[1]);
						for ($i = 0; $i < $len; $i++)
							$retval["summary"][] = array( $reg[1][$i] , $reg[2][$i] );
					}
				}
			}
			
			return( $retval );
		}
		return( null );
	}