Skip to content

Instantly share code, notes, and snippets.

@TimWolla
Last active December 11, 2015 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimWolla/4573660 to your computer and use it in GitHub Desktop.
Save TimWolla/4573660 to your computer and use it in GitHub Desktop.
<?php
$input = "root(27,3)";
$number = '(?:-?\d+(?:\.\d+)?)';
while (!preg_match('/^'.$number.'$/', $input)) {
$input = preg_replace_callback('/\(\s*('.$number.')\s*\)/', function ($matches) {
return $matches[1];
}, $input);
$input = preg_replace_callback('/('.$number.')\s*\^\s*('.$number.')/', function ($matches) {
return pow($matches[1], $matches[2]);
}, $input);
$input = preg_replace_callback('/root\(\s*('.$number.')\s*,\s*('.$number.')\s*\)/', function ($matches) {
return pow($matches[1], 1 / $matches[2]);
}, $input);
$input = preg_replace_callback('/sqroot\(\s*('.$number.')\s*\)/', function ($matches) {
return pow($matches[1], 0.5);
}, $input);
$input = preg_replace_callback('/('.$number.')\s*\*\s*('.$number.')/', function ($matches) {
return $matches[1] * $matches[2];
}, $input);
$input = preg_replace_callback('/('.$number.')\s*\/\s*('.$number.')/', function ($matches) {
return $matches[1] / $matches[2];
}, $input);
$input = preg_replace_callback('/('.$number.')\s*\+\s*('.$number.')/', function ($matches) {
return $matches[1] + $matches[2];
}, $input);
$input = preg_replace_callback('/('.$number.')\s*-\s*('.$number.')/', function ($matches) {
return $matches[1] - $matches[2];
}, $input);
echo $input."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment