我需要抢出来的话这个字符串并将其替换AND LIKE/NOT在这样一个SQL语句,但我拉我的头发试图弄明白:-)PHP preg_match_all语法/正则表达式
$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;
这工作,谢谢! – 2012-04-06 23:45:43
非常欢迎。我花了很长时间才得到正则表达式。如果您愿意将此答案标记为正确答案,我将不胜感激。 – Odyssey 2012-04-07 00:31:58