How to parse a string of boolean logic in PHP

12,803

Solution 1

Just stumbled upon this question, but being fairly uneasy about using eval, I decided to keep looking for a better solution.

What I discovered is yet another wonderful use for PHP's filter_var function, when passing in the FILTER_VALIDATE_BOOLEAN flag (of which there are many).

This "one line" function seems to do well at safely converting a string (or other) object to a boolean:

<?php

/**
 * Uses PHP's `filter_var` to validate an object as boolean
 * @param string $obj The object to validate
 * @return boolean
 */
function parse_boolean($obj) {
    return filter_var($obj, FILTER_VALIDATE_BOOLEAN);
}

And, a little testing:

/**
 * Let's do some testing!
 */
$tests = array (
    "yes",
    "no",
    "true",
    "false",
    "0",
    "1"
);

foreach($tests as $test) {

    $bool = parse_boolean($test);

    echo "TESTED: ";
    var_dump($test); 

    echo "GOT: ";
    var_dump($bool);

    echo "\n\n";

}

Output:

/*
TESTED: string(3) "yes"
GOT: bool(true)


TESTED: string(2) "no"
GOT: bool(false)


TESTED: string(4) "true"
GOT: bool(true)


TESTED: string(5) "false"
GOT: bool(false)


TESTED: string(1) "0"
GOT: bool(false)


TESTED: string(1) "1"
GOT: bool(true)
*/

I haven't looked deep enough, but it's possible that this solution relies on eval down the line somewhere, however I'd still side with using those over plain evaling since I assume that filter_var would also handle sanitizing any input before piping it through eval.

Solution 2

eval() will work perfectly fine for this, but remember you have to tell it to return something.

$string = "true && true || false";
$result = eval("return (".$string.");"); // $result will be true

Also make sure you sanitize any user inputs if putting them directly into an eval().

Solution 3

Let's assume eval() is an ok/good solution in your case.

class Foo {
  private function trustworthy() {
    return 'true && true || false';
  }

  public function bar() {
    return eval('return '.$this->trustworthy().';');
  }
}

$foo = new Foo;
$r = $foo->bar();
var_dump($r);

prints bool(true)

Share:
12,803
TheOddLinguist
Author by

TheOddLinguist

I write software to make life easier...or more fun (which, in some cases, is the same thing)

Updated on June 05, 2022

Comments

  • TheOddLinguist
    TheOddLinguist almost 2 years

    I'm building a PHP class with a private member function that returns a string value such as:

    'true && true || false'
    

    to a public member function. (This string is the result of some regex matching and property lookups.) What I'd like to do is have PHP parse the returned logic and have the aforementioned public function return whether the boolean result of the parsed logic is true or false.

    I tried eval(), but I get no output at all. I tried typecasting the boolean returns...but there's no way to typecast operators...hehe Any ideas? (Let me know if you need more information.)

  • TheOddLinguist
    TheOddLinguist almost 14 years
    Thanks; I was forgetting the 'return'. The string is the result of a rather tight regex, so I think I'll be fine. I'll re-assess the sanitizing now that this is working. Thanks again.
  • TheOddLinguist
    TheOddLinguist almost 14 years
    Thanks for your help; I was indeed forgetting the 'return'...hehe
  • Ruhee Jaiswal
    Ruhee Jaiswal almost 12 years
    This doesn't really solve the original problem (in that operators are not handled). filter_var('true && true', FILTER_VALIDATE_BOOLEAN) returns false.
  • gpmcadam
    gpmcadam almost 12 years
    Absolutely. This was a misunderstanding of the original question on my part, and should be unmarked as the accepted solution.