格式化一个搜索字符串

function parse_filter($filter_string) {
  $filter_pair = [];
  if (!!$filter_string) {
    $filter_a = explode(' ', trim($filter_string));
    foreach ($filter_a as $f) {
      if (!$f) {
        continue;
      }
      $f_a = explode(':', $f, 2);
      if (count($f_a) > 1) {
        if (!$f_a[1]) {
          continue;
        }
        $filter_pair[] = $f_a;
      } else {
        $filter_pair[] = ['query', $f];
      }
    }
    unset($f);
  }

  return $filter_pair;
}

eg parse_filter('is:issue s is:open foo: hh ');返回:

array (size=4)
  0 => 
    array (size=2)
      0 => string 'is' (length=2)
      1 => string 'issue' (length=5)
  1 => 
    array (size=2)
      0 => string 'query' (length=5)
      1 => string 's' (length=1)
  2 => 
    array (size=2)
      0 => string 'is' (length=2)
      1 => string 'open' (length=4)
  3 => 
    array (size=2)
      0 => string 'query' (length=5)
      1 => string 'hh' (length=2)

Leave a comment

* - Required fields