php preg_match用于分面搜索字符串

时间:2011-12-04 16:50:10

标签: php preg-match

我正在寻找php preg_match来打破分面搜索字符串。

示例搜索输入: chevy tahoe city:西雅图颜色:沙漠沙丘容量:7

  • 搜索条件:chevy tahoe
  • city facet:seattle
  • 颜色方面:沙漠沙丘
  • capacity facet:7

搜索条件始终是输入字符串中的第一个...但是构面可以互换。

1 个答案:

答案 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
)