php preg_match_all语法/正则表达式

时间:2012-04-06 21:26:29

标签: php

我需要从这个字符串中取出单词并在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;

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:1)

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

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

您的表达式目前仅捕获短划线后面的单词。

循环也不正确。你想要像

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

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