Using PHP, I try to improve the search on my site, supporting Google, for example, operators, for example.
- keyword = natural / default
- "keyword" or "search phrase" = exact match
- keyword * = partial match
To do this, I need to split the string into two arrays. One for exact words (but without double quotes) in $ Array1 () and puts everything else (natural and partial keywords) in Array2 ().
What regular expressions will achieve this for the next line?
Example string ($ string)
Today I am "trying" to do a "google search" "test"
Desired Result
$Array1 = array( [0]=>trying [1]=>google search [2]=>testing ); $Array2 = array( [0]=>today [1]=>i'm [2]=>out [3]=>a* );
1) Exact I tried the following for the exact regexp, but it returns two arrays: one with one and without double quotes. I could just use $ result [1], but there might be a trick I miss here.
preg_match_all( '/"([^"]+)"/iu', 'today i\'m "trying" \'out\' a* "google search" "test"', $result );
2) Natural / partial The following rule returns the correct keywords, but together with a few empty values. Can this regex rule be messy or should I just start the array through array_filter ()?
preg_split( '/"([^"]+)"|(\s)/iu', 'today i\'m "trying" \'out\' a* "google search" "test"' );
php regex
Adam
source share