-1

I'm building a website using PHP.

I am using a preg_split() to separate a given string which looks like +word1 -word2 -"word word". But I need them in the following form +word1, -word2, -"word word".

Currently, I have this one:

$words = preg_split("/[\s\"]*\"([^\"]+)\"[\s\"]*|" . "[\s\"]*'([^']+)'[\s\"]*|" . "[\s\"]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

but it didn't work as I wish: I need to do it in this way to get it work: +word1 -word2 '-"word word"'.

Does someone have a better regex or idea?

2 Answers2

2

One option is to match from a double quote till a double quote and don't split on that match using SKIP FAIL. Then match 1+ horizontal whitespace chars to split on.

"[^"]+"(*SKIP)(*FAIL)|\h+

Regex demo | Php demo

For example

$search_expression = '+word1 -word2 -"word word"';
$words = preg_split("~\"[^\"]+\"(*SKIP)(*FAIL)|\h+~", $search_expression);
print_r($words);

Output

Array
(
    [0] => +word1
    [1] => -word2
    [2] => -"word word"
)
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

A simpler expression with greedy ? works for matching your examples:

preg_match_all('/[+-][^+-]+ ?/', $search_expression, $matches);
print_r($matches[0]);

Yields:

Array
(
    [0] => +word1 
    [1] => -word2 
    [2] => -"word word"
)

Se Example.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87