【发布时间】:2013-08-01 20:35:01
【问题描述】:
我正在使用我在this question 上找到的答案来允许我拥有搜索运算符。
它的一个问题是它要求 then 运算符和单词之间没有空格。
所以它将匹配 operator:something 但不匹配 operator: something 所以我想知道如何匹配任何一种形式?
preg_match_all('/
(?:
([^: ]+) # command
: # trailing ":"
)
(
[^: ]+ # 1st word
(?:\s+[^: ]+\b(?!:))* # possible other words, starts with spaces, does not end with ":"
)
/x',
$search, $matches, PREG_SET_ORDER);
$result = array();
foreach($matches as $match) {
$result[$match[1]] = isset($result[$match[1]])
? $result[$match[1]] . ' ' . $match[2]
: $match[2];
}
【问题讨论】:
-
您在 Savannah 提供的代码中所做的更改,您只是在问答案而没有对此进行处理
-
您必须发布输入字符串和预期的输出,而不是指向不良书面帖子的链接。谢谢。
标签: php regex search operators preg-match-all