title: PHP Validate that a string is a mathematical expression
body: <p>I am trying to write a PHP function to check if a string represents a mathematical expression.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>function is_math( $maybe_math ){
      $pattern = '([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9])';
      if( 0 === preg_match( $maybe_math, $pattern ) ){
          return true;
      }
  }
  </code></pre>
  
  <p>I am testing with these possibilities:</p>
  
  <pre><code>$valid = is_math( '(1-4*(9*5/1)+1)' ); //true
  $invalid = is_math( 'hats'  ); //false
  $valid_and_invalid = is_math( '(1-4aaa*(9*5/1)+1k)' ); //true
  </code></pre>
  
  <p>As you can see, so far it evaluates true for a mathematical expression, or a string with valid math characters and invalid math characters. Right now the third test I am showing should be false, since there are letters in it and I am not doing algebra here.</p>
  
  <p>My goal here is to create a function I can use to validate that my string can be passed to eval and get a number back and not risk execution of unsafe code, as these numbers come partially from POST data. </p>