我需要搶出來的話這個字符串並將其替換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