如何解析表示布尔值的字符串

发布于 2024-08-31 07:59:49 字数 294 浏览 3 评论 0原文

我正在构建一个带有私有成员函数的 PHP 类,该函数返回一个字符串值,例如:

'true && true || false'

到公共成员函数。 (这个字符串是一些正则表达式匹配和属性查找的结果。)我想做的是让 PHP 解析返回的逻辑,并让前面提到的公共函数返回解析逻辑的布尔结果是 true 还是 false。

我尝试了 eval(),但根本没有输出。我尝试对布尔返回进行类型转换...但是无法对运算符进行类型转换...呵呵有什么想法吗? (如果您需要更多信息,请告诉我。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

默嘫て 2024-09-07 07:59:50

刚刚偶然发现了这个问题,但由于对使用 eval 相当不安,我决定继续寻找更好的解决方案。

我发现 PHP 的另一个奇妙用途 filter_var< /a> 函数,当传入 FILTER_VALIDATE_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);
}

并且,进行了一些测试:

/**
 * 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";

}

输出:

/*
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)
*/

我还没有看起来足够深入,但这个解决方案可能依赖于某个地方的 eval ,但是我仍然倾向于使用那些普通的 eval ,因为我假设 < code>filter_var 还将在通过 eval 管道传输之前处理任何输入的清理。

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.

ˉ厌 2024-09-07 07:59:50

eval() 对此可以完美地工作,但请记住您必须告诉它返回一些内容。

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

如果将任何用户输入直接放入 eval() 中,还要确保对其进行清理。

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().

ㄟ。诗瑗 2024-09-07 07:59:50

让我们假设 eval() 对于您的情况来说是一个好的/好的解决方案。

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);

打印 bool(true)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文