我有一个像这样的字符串: $string='(orange fruit OR APPLE) AND (Kiwi OR banana pie)'
我需要使用正则表达式来提取由布尔运算符分隔的单词OR
,AND
并忽略方括号。
所以结果应该是一个数组: orange fruit, APPLE, Kiwi, banana pie
我不确定从哪里开始,因为我对正则表达式不太满意。有没有简单的方法可以做到这一点?
在当前情况下,请执行以下操作:
$string =' ( orange fruit OR APPLE ) AND (Kiwi OR banana pie)';
// Remove all extra characters and spaces from string
$result = trim(preg_replace('#[\s\(\)]+#', ' ', $string));
// Split result by possible delimeters
$result = preg_split('# (or|and) #i', $result);
// Output
print_r($result);
输出:
Array (
[0] => orange fruit
[1] => APPLE
[2] => Kiwi
[3] => banana pie
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句