1

我需要从这个字符串中取出单词并在 sql 语句中用 AND LIKE/NOT LIKE 替换,但我正在努力弄清楚:-)

$string = "hello -world !someword ! again |foo &foo AND foo ORfoo";
if (preg_match_all("/AND|OR|\||&|!|\-(\w+)/", "$string", $matches)) {
        foreach ($matches as $match) {
                foreach ($match as $m) {
                        // 1. figure out what the delimiter was (!, |, &, -, AND, OR
                        // 2. create a sql statement from it using the word following the delimiter
                        // Example:
                        if ($m = "!") {
                                $where .= " AND msg NOT like '%".$m."%'";
                        }
                        if ($m = "AND") {
                                $where .= " AND msg like '%".$m."%'";
                        }
                        if ($m = "OR") {
                                $where .= " OR msg like '%".$m."%'";
                        }
                }
        }
}
echo $where;
4

2 回答 2

2

使用下面的代码来收集您的正则表达式匹配。请记住,preg_match_all 旨在收集给定模式的所有出现。请注意,我使用 preg_match 代替。"\s*" 允许您匹配存在的空格。

$results = array();
$word_to_capture = '\s*(\w+)%';
$patterns[] = "%(AND)" . $word_to_capture;
$patterns[] = "%(&)" . $word_to_capture;
$patterns[] = "%(OR)" . $word_to_capture;
$patterns[] = "%(\|)" . $word_to_capture;
$patterns[] = "%(!)" . $word_to_capture;
$patterns[] = "%(-)" . $word_to_capture;

foreach($patterns as $p):
    preg_match($p,$string,$matches);
    $results[] = $matches;
endforeach;

然后你可以遍历 $results 数组来创建你的 MySQL 查询。生成的数组将如下所示。

Array
(
    [0] => Array
        (
            [0] => AND foo
            [1] => AND
            [2] => foo
        )

    [1] => Array
        (
            [0] => &foo
            [1] => &
            [2] => foo
        )

    [2] => Array
        (
            [0] => ORfoo
            [1] => OR
            [2] => foo
        )

    [3] => Array
        (
            [0] => |foo
            [1] => |
            [2] => foo
        )

    [4] => Array
        (
            [0] => !someword
            [1] => !
            [2] => someword
        )

    [5] => Array
        (
            [0] => -world
            [1] => -
            [2] => world
        )

)

我在正则表达式中添加了一个额外的捕获。您的不同模式将在元素 1 中,单词将在元素 2 中。

您可以使用下面的代码来创建您的查询片段:

$query_fragment = "";
foreach($results as $r):
    switch (trim($r[1])):
        case "AND":
            $query_fragment .= " AND msg LIKE '%" . $r[2] . "%'";
            break;
        case "!":
            $query_fragment .= " AND msg NOT LIKE '%" . $r[2] . "%'";
            break;
        case "&":
            $query_fragment .= " AND msg LIKE '%" . $r[2] . "%'";
            break;
        case "OR":
            $query_fragment .= " OR msg LIKE '%" . $r[2] . "%'";
            break;
        case "|":
            $query_fragment .= " OR msg LIKE '%" . $r[2] . "%'";
            break;
    endswitch;
endforeach;
于 2012-04-06T22:09:24.040 回答
1

您需要单独捕获触发器。那是:

/(AND|OR|\|&|!-)(\w+)/

您的表达式当前仅捕获一个破折号后的单词。

循环也不正确。你想要类似的东西

foreach ($matches[1] as $count => $match) {
   if ($match == '!') {
      $where . = " AND msg NOT LIKE '%" . $matches[2][$count] . "%'";
   }
}

SQL 注入也可能是您关心的问题。

于 2012-04-06T22:03:30.297 回答