-2

I am using the answer I found on this question to allow me to have search operators.

The one issue with it is that it requires that there are no spaces between then operator and the words.

So it will match operator:something but not operator: something so what I am wondering is how I can match either form?

preg_match_all('/
  (?:
    ([^: ]+) # command
    : # trailing ":"
  )
  (
    [^: ]+  # 1st word
    (?:\s+[^: ]+\b(?!:))* # possible other words, starts with spaces, does not end with ":"
  )
  /x',
  $search, $matches, PREG_SET_ORDER);

  $result = array();
  foreach($matches as $match) {
      $result[$match[1]] = isset($result[$match[1]]) 
          ? $result[$match[1]] . ' ' . $match[2] 
          : $match[2];
  }
Community
  • 1
  • 1
Hailwood
  • 89,623
  • 107
  • 270
  • 423

2 Answers2

1

4th line

:\ ? # trailing ":" or ": "
klkvsk
  • 670
  • 4
  • 7
1
:\s*

\s is a flag for white-spaces, * means 0 or more

Hubu
  • 59
  • 2