3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $str = "(联想 OR 戴尔 OR 苹果 OR 宏基) dangers:deep1条 dangers:(newuuid deep1条 FirstTimeImageRule) 价格_i:[1500 TO 2500] cityEnglishName:beijing categoryEnglishName:bijiben"; if (preg_match_all('/([a-zA-Z]+)\:\(([^\)]+)\)/', $str, $m)) { for ($i = 0; $i < count($m[0]); $i++) { $q = trim($m[0][$i]); if (strlen($q) == 0) continue; $key = $m[1][$i]; $qq = explode(" OR ", $m[2][$i]); if (count($qq) > 1) { $op = " OR "; } else { $qq = explode(" AND ", $m[2][$i]); if (count($qq) <= 1) { $qq = explode(" ", $m[2][$i]); } $op = " AND "; } foreach ($qq as $k => $v) { $qq[$k] = "{$key}:{$v}"; } $str = str_replace($q, "(" . implode($op, $qq) . ")", $str); } } var_dump($str);
Output for 4.3.0 - 7.1.0
string(201) "(联想 OR 戴尔 OR 苹果 OR 宏基) dangers:deep1条 (dangers:newuuid AND dangers:deep1条 AND dangers:FirstTimeImageRule) 价格_i:[1500 TO 2500] cityEnglishName:beijing categoryEnglishName:bijiben"