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