我正在寻找php preg_match来打破分面搜索字符串。
示例搜索输入: chevy tahoe city:西雅图颜色:沙漠沙丘容量:7
搜索条件始终是输入字符串中的第一个...但是构面可以互换。
答案 0 :(得分:0)
试试这个。它应该工作。
$criteria = '';
$facets = array();
foreach (explode(' ' , $input) as $word) {
// This word is a facet
if (strpos($word, ':')) {
list($facet_name, $facet_criteria) = explode(':', $word, 1);
$facets[$facet_name] = $facet_criteria;
// This word is a search criteria
} else {
$criteria .= $word . ' ';
}
}
输入($input
值):
chevy tahoe city:seattle color:desert dune capacity:7
输出:
$criteria: chevy tahoe dune
$facets: Array (
[city] => seattle
[color] => desert
[capacity] => 7
)