Top

Google Calculator


PHP Code
+ 1 likes
Please Register to submit score.
Bookmark and Share
Average Score  6.0 (of 1 scores)
Date Added  Mar 04, 2009
Last Updated  Jun 16, 2009
Tags  bot  calc  calculator  php 

Introduction

This just uses googles own calculator..

heres what the output looks like

[12:49][RandomUser] !calc 23*4/56^2/456+8999
[12:49][PHP_Bot] (((23 * 4) / (56^2)) / 456) + 8999 = 8999.00006

full instructions here:
http://www.google.co.uk/intl/en/help/calculator.html

Grab the Code

<?php
 
// Google calculator
function do_calculator($query){
	if (!empty($query)){
		$url = "http://www.google.co.uk/search?q=".urlencode($query);
		$f = array("Â", "<font size=-2> </font>", " &#215; 10", "<sup>", "</sup>");$t = array("", "", "e", "^", "");
		preg_match('/<h2 class=r style="font-size:138%"><b>(.*?)<\/b><\/h2>/', file_get_contents($url), $matches);
		if (!$matches['1']){
			return 'Your input could not be processed..';
		} else {
			return str_replace($f, $t, $matches['1']);
		}
	} else {
		return 'You must supply a query.';
		}
	}
 
?>

Comments

  (4)  RSS
Hawkee
Comments: 1,039
 
PHP Snippet:  Google Calculator
Posted on Mar 4, 2009 12:52 pm
I like how you've expanded on an existing snippet, but this may be a bit advanced for some users. You've got a handful of include statements for files nobody has access to, so that might confuse some folks. Overall this is a helpful addition.
F*U*R*B*Y*
Comments: 637
 
PHP Snippet:  Google Calculator
Posted on Mar 4, 2009 7:35 pm
nice....

I'd use switch to, if I did do it again....

but what I've done is just had includes for all the files/commands

=)
bone282
Comments: 31
 
PHP Snippet:  Google Calculator
Posted on Mar 8, 2009 10:59 pm
thx for the positive input, ive tidied the snippet up alot aswell. i made in the first couple weeks of learning php. and yeah furby using switch is clean looking and easy on your head.
bone282
Comments: 31
 
PHP Snippet:  Google Calculator
Posted on Mar 9, 2009 9:40 am
i edited it again and went back to the old str_replace...

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom