【问题标题】:is there a php wildcard boolean filter function是否有 php 通配符布尔过滤功能
【发布时间】:2016-02-01 00:26:33
【问题描述】:

是否有一个同时进行通配符过滤和布尔 AND/OR 过滤的 php 函数?

$source = "My dog has fleas";
$filter = "*fleas OR Your*";
echo "Result: ".WildcardBooleanSearch($source, $filter);

结果应该是真或假。在这个例子中,它应该说是真的。 我在网上搜索,找到通配符过滤,找到布尔过滤,但我没有找到两者合二为一。

fnmatch($filter,$source) 处理通配符,但不知道 AND 和 OR 是什么意思。与 preg_match 相同的限制。 我不能简单的 explode(' or ',$filter) 因为可能有嵌套的 OR 和 AND。

我能想到的最接近的比较是VBA函数Application.BuildCriteria

【问题讨论】:

    标签: php string search filter


    【解决方案1】:

    使用 PHP preg_match 函数。如果您使用简单的字符串搜索,您也可以使用 strpos。这是一个例子:

    $source="My dog has fleas";
    $filter="fleas or your";
    if (strpos($source,$filter)) {
       echo "Found the match";
    }
    

    您可以使用 preg_match 进行更复杂的匹配。

    【讨论】:

    • 不,那行不通。 $filter 中的“或”一词是一个布尔 OR,而不是纯英语的“或”。
    • 嗯,你是对的......这不适用于布尔或。你最好使用 preg_match ,你可以使用正则表达式来匹配几乎任何东西。
    【解决方案2】:

    或:

    echo "Result: " . preg_match( "/(fleas|Your)/", $source );
    

    和:

    echo "Result: " . ( preg_match( "/fleas/", $source ) && preg_match( "/Your/", $source ) );
    

    【讨论】:

    • 不,那行不通。必须使用 WildcardBooleanSearch($source, $filter); 形式的函数调用计算结果;
    • 嗯,这只是你如何在你的函数中实现这些 preg_matches 的问题。您可以在那里添加更多搜索模式,或者就这样保留它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2012-05-05
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多