Evaluate Maths in PHP

A few days ago, I had to need to evaluate mathamatical expressions entered by the users of the site. This could potentially cause security issue when using the PHP function eval(); so I decided to tackle the problem in a different way.

After some research, I came across a Eval Math by Miles Kaufmann. It has not been updated since 2005 and is slightly out of date. So I have decided to create a new open source project to create a better class.

As it is at the moment, initially I had no idea how to approach the problem. The first thing i needed to understand was how computers can evaluate mathematical expressions.

Eventually I came across infix notation and postfix notation. Only getting to A level Maths, I had no idea what they were. After battling through some heavy maths, I realised infix is the way we normally write expressions

Infix: 3 + 4 – 2
Postfix: 3 4 + 2 -

At first it looks odd but once explained it is simple. Here how it works:

  1. (3) is a number.
  2. (4) is another number
  3. (+) is an operation so take the previous 2 numbers (3 + 4 = 7).
    So the expression becomes: 7 2 - 
  4. (7) is a number.
  5. (2) is a number.
  6. (-) is an operation so take the previous 2 numbers (7 – 2 = 5).
  7. Only one number left, that is the answer (5).
  8. So “3 + 4 – 2″ = “3 4 + 2 -” = 5

This is a simple example but the point is to make the expression easier for a computer to evaluate. Postfix removes the need for brackets in expression as precedence is sort by the order of the numbers and operations.

Few more examples:

Infix: -2 * (a + 5) + (-2 + 2)
Postfix: -2 a 5 + * -2 2 + + 

Infix: 2a^2 + 2a + 2
Postfix: 2 a 2 ^ * 2 a * + 2 +

Once I understood this, I started writing an algorithm to convert infix to postfix using this wiki page http://en.wikipedia.org/wiki/Shunting-yard_algorithm.

Understanding the algorithm was alittle difficult at first but I eventually got my head around it.

And so I managed to get a working class that will evaluate math expressions.

The code is hosted at https://bitbucket.org/maca134/mathevaluator and anyone is welcome to contribute to the code.

At the moment the class will handle the following operations:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Power (^)
  • Modulus (%)
  • Parentheses
Here is an example of the code:
$results = MathEvaluator::e('2a + (12 / 2) - b * -2', 
	array('a' => 2, 'b' => 4)
);
 
array(4) {
	["result"]=>
	float(18)
	["infix"]=>
	string(22) "2a + (12 / 2) - b * -2"
	["postfix"]=>
	string(23) "2 a * 12 2 / + b -2 * -"
	["vars"]=>
	array(4) {
		["a"]=>
		string(1) "2"
		["b"]=>
		string(1) "4"
	}
}

23. January 2012 by maca134
Categories: Blog | Leave a comment

WP Simple Galleries

A simple plugin that adds an image gallery to each post and page. Most gallery plugins seem to be fancy and advanced but sometimes you just want a simple solution to add images to a post of page.

I created this to solve that problem. The plugin uses the WordPress image manager, so you can add already uploaded images too a gallery.

Download: http://wordpress.org/extend/plugins/wp-simple-galleries/

See demo below.

19. December 2011 by maca134
Categories: Blog | Leave a comment

Shopp Customer Register

Shopp Customer Register is a WordPress plugin I created, when I found that Shopp (another WordPress plugin) does not have a user/customer register feature, which I thought was abit silly. So I set about creating a small plugin to fill the gap!

It was become more popular than I originally anticipated. So I have decide to devote some more time and add some extra features, as people seem to want them.

The plugin basicly has a shortcode that inserts a registration form into any page/post.

Check it out at http://wordpress.org/extend/plugins/shopp-customer-register

Here is my Bitbucket repository https://bitbucket.org/maca134/shopp-customer-register/wiki/Home

02. December 2011 by maca134
Categories: Blog | Leave a comment

Tweeter Karma – Unfollow all non-followers

Tweeter Karma is a great little tool for showing and filtering your Twitter followings and followers but apparently recently they had to remove there ‘Unfollow All’ feature at Twitter’s request. Which for some people, not a problem but if you have used one of them Twitter tools, you can have hundreds of followings and a disproportionate number of followers.

So in true hacker style I have come up with a small Javascript snippet that will unfollow everyone on the page.

All it does is look for the Unfollow links and triggers the click event. Wait a second. Repeat.

function pausecomp(ms) {
	ms += new Date().getTime();
	while (new Date() < ms){}
}
$('.twitter-users li').each(function() {
	$(this).find('div.action a').click();
	pausecomp(1000);
});

Just to make life abit easier, there is also a nice bookmarklet below, just drag it to your bookmark bar: Unfollow All

17. November 2011 by maca134
Categories: Blog | Leave a comment

New Site

Just getting my new site up and running. More to come!

16. November 2011 by maca134
Categories: Blog | Leave a comment