在几个条件下拆分文本(PHP)

时间:2016-07-25 17:52:36

标签: php mysql sql

我有一个表单输入字段'text',我想根据用户在该字段中输入的值创建不同的查询

  1. 如果只有一个短语 - 搜索每个单词(f.e.'Hello World'):

    SELECT (...) WHERE x LIKE '%Hello%' AND x LIKE '%World%' etc...
    
  2. 如果短语在引号中 - 搜索整个短语(f.e。'“Hello World”'):

    SELECT (...) WHERE x LIKE '%Hello World%'
    
  3. 这很酷 - 我可以做到。

    但是当我必须混合以上功能时,我的问题就开始了 - 所以f.e.如果短语是'Hello World',我的名字是“John” - 它应该像这样搜索:

    SELECT (...) 
    WHERE x LIKE '%Hello%' 
        AND x LIKE '%World%' 
        AND x LIKE '%my name is%' 
        AND x LIKE '%John%'
    

    你将如何实现这样的功能并设法在php中执行此操作?

2 个答案:

答案 0 :(得分:2)

您可以使用preg_match_all(...):

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic nil_RF2_P2  --consumer.config config/consumer1.properties 
sarkar
hello all again!!

将产生:

$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \\"elit" dolor';
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $text, $matches);
print_r($matches);

正如您所看到的,它还说明了引用字符串中的转义引号。

一个简短的解释:

Array
(
    [0] => Array
        (
            [0] => Lorem
            [1] => ipsum
            [2] => "dolor sit amet"
            [3] => consectetur
            [4] => "adipiscing \"elit"
            [5] => dolor
        )

)

如果匹配%22而不是双引号,您可以:

"           # match the character '"'
(?:         # start non-capture group 1 
  \\        #   match the character '\'
  .         #   match any character except line breaks
  |         #   OR
  [^\\"]    #   match any character except '\' and '"'
)*          # end non-capture group 1 and repeat it zero or more times
"           # match the character '"'
|           # OR
\S+         # match a non-whitespace character: [^\s] and repeat it one or more times

您也可以查看this

答案 1 :(得分:0)

您可以在preg_replace()中使用preg_split()preg_replace_callback,并使用一些声明variables来构建您的查询。下面的代码片段显示了如何:

<?php

    $str        = 'Hello World, "My Name is" and "her name is"';
    $matches    = array();
    $result     = preg_replace_callback("#\".*?\"#", function($match) use(&$matches, &$str) {
        $matches[]  = trim($match[0], ",.:?;\"");
        $res        = preg_split("#\s.*?#", preg_replace("#\".*?\"#", "", $str));
        $matches    = array_merge($matches, $res);
        $matches    = array_unique(array_filter(array_merge($matches, $res)));
    }, $str);

    $query      = "SELECT * FROM `tbl_name` AS tbl WHERE ";
    foreach($matches as $searchTerm){
        $query .= " tbl.x LIKE '%" . $searchTerm . "%' OR ";
    }
    $query      = rtrim($query, " OR ");


    var_dump($query);
    var_dump($matches);
上面的2个var_dumps():var_dump($query)var_dump($matches)分别产生:
    string 'SELECT * FROM `tbl_name` AS tbl WHERE  tbl.x LIKE '%My Name is%' OR  tbl.x LIKE '%Hello%' OR  tbl.x LIKE '%World,%' OR  tbl.x LIKE '%and%' OR  tbl.x LIKE '%her name is%'' (length=169)

    array (size=5)
      0 => string 'My Name is' (length=10)
      1 => string 'Hello' (length=5)
      2 => string 'World,' (length=6)
      3 => string 'and' (length=3)
      4 => string 'her name is' (length=11)