1

Having pretty much covered the basics in PHP, I decided to challenge myself and make a simple calculator. After some attempts I figured it out, but I'm not entirely content with it. I want to make it more user friendly and have the calculator input in just one box, very much like google search.

So one would simply type: 5+2 and recieve 7.

How would I split the string "5+2" into three variables so that the math functions can convert the numbers into integers and recognize the operator, as well as accounting for the possibility of someone using spaces between the values as well?

Would you explode the string? But what would you explode it with if there are no spaces? I've also stumbled upon the preg_split function, but I can't seem to wrap my head around or know if it's suitable to solve this problemt. What method would be the best option for this?

Why
  • 27
  • 3
  • Just loop through all characters in the string and figure out what is what. – gen_Eric Dec 07 '10 at 22:14
  • For curiosity's sake, how would you go about to do that in an example? – Why Dec 07 '10 at 22:24
  • Exactly what do you plan to do with the data once it's been separated? I would suggest making it a bit of a lesser challenge and cooler (IMO) by creating a javascript calculater complete with buttons so that everything is already separated when posted. It's much harder to give them free range with a text box because then you're well on your way to making a math parser which can be very difficult (there may be existing parsers that would help with that, however -- such as PHPExcel). – webbiedave Dec 07 '10 at 22:30
  • Example: if($operator == '+') { ($number1+$number2); } The values would be assigned to the variables after the split. Just figured I'd be something cool to do, and just learn some new stuff. – Why Dec 07 '10 at 22:34

4 Answers4

2
$calc = "5* 2+ 53";
$calc = preg_replace('/(\s*)/','',$calc);
print_r(preg_split('/([\x28-\x2B\x2D\x2F])/',$calc,-1,PREG_SPLIT_DELIM_CAPTURE));

That's my bid, resulting in

Array
(
    [0] => 5
    [1] => *
    [2] => 2
    [3] => +
    [4] => 53
)
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

You may need to use some clever regex to split it something like:

$myOutput = split("(-?[0-9]+)|([+-*/]{1})|(-?[0-9]+)");

I haven't tested that - just an semi-psuedo-ish example sorry :-> just trying to highlight that you will need to remember that your - (minus) operator can appear at the start of an integer to make it a negative number so you could end up with problems with things like -1--21 which is valid but makes your regex rules more complicated.

Rob
  • 10,004
  • 5
  • 61
  • 91
0

You will have to split the string using regular expressions. For example a simple regex for 5+2 would be:

\d\+\d

Check out this link. You can create and validate your regular expressions there. For a calculator it will not be that difficult.

tHeSiD
  • 4,587
  • 4
  • 29
  • 49
  • Great resource, thanks! Finally something that can help me make sense of regular expressions. – Why Dec 07 '10 at 22:25
  • While this is probably the best answer you can get, I just want to point out that PHP is not really built for low level stuff like that. That being said, a "perfect" (as in "major security hole") calculator for PHP would be $result = eval($expression) – vassilis Dec 07 '10 at 22:27
  • Along with what @vassilis said, time would be better spent making a regex that validates expressions for actual math expressions then passing them off to eval(). – Brad Christie Dec 07 '10 at 22:36
  • I suppose I could do that too. – Why Dec 07 '10 at 22:39
0

You've got the right idea with preg_split. It would work something like this:

$values = preg_split("/[\s]+/", "76  +    23");

The resulting array will contain values that are NOT whitespace:

Values should look like this: $values[0]: "76" $values[1]: "+" $values[2]: "23"

the "/[\s]+/" is a regular expression pattern that matches any whitespace characters one or more times. Howver, if there are no whitespaces at all, preg_split will just return the original "5+2" as a single string in the first element of the array. i.e.:

$values[0] = "5+2"
Ryan Anderson
  • 2,212
  • 2
  • 14
  • 10