Ana*_*Ban 7 php regex string parsing
例如,我需要在php中解析关键字和短语的搜索字符串
字符串1: value of "measured response" detect goal "method valuation" study
会产生: value,of,measured reponse,detect,goal,method valuation,study
如果字符串有:我也需要它工作:
我倾向于使用preg_match模式'/(\".*\")/'将短语放入数组中,然后从字符串中删除短语,最后将关键字放入数组中.我不能把所有东西拉到一起!
我也在考虑用逗号替换引号之外的空格.然后将它们分解为数组.如果这是一个更好的选择,我该怎么做preg_replace?
有没有更好的方法来解决这个问题?救命!非常感谢大家
Fai*_*Dev 10
preg_match_all('/(?<!")\b\w+\b|(?<=")\b[^"]+/', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
# Matched text = $result[0][$i];
}
Run Code Online (Sandbox Code Playgroud)
这应该产生您正在寻找的结果.
说明:
# (?<!")\b\w+\b|(?<=")\b[^"]+
#
# Match either the regular expression below (attempting the next alternative only if this one fails) «(?<!")\b\w+\b»
# Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!")»
# Match the character “"” literally «"»
# Assert position at a word boundary «\b»
# Match a single character that is a “word character” (letters, digits, etc.) «\w+»
# Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
# Assert position at a word boundary «\b»
# Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «(?<=")\b[^"]+»
# Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=")»
# Match the character “"” literally «"»
# Assert position at a word boundary «\b»
# Match any character that is NOT a “"” «[^"]+»
# Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4190 次 |
| 最近记录: |