I'm building a filter system with PHP. I need to give a priority on a text based on keywords matches. The filter has to recognize different types of keywords.
One of the types is normal words, keyword1 keyword2
. This would filter on texts with both 'keyword1' and 'keyword2', no matter what order of whether they occur consecutively in the text or not.
An other type is a precise combination of words, "keyword1 keyword2"
. This would give priority to articles with the exact combination "keyword1 keyword2".
There are other types but they aren't relevant here.
Keyword types may be combined, so keyword1 "keyword2 keyword3"
is valid and would search for articles with both "keyword1" and the exact combination "keyword2 keyword3".
For the first type, I can use an explode(' ', $keywords)
to get the keywords in an array. However, this would mess up with keyword1 "keyword2 keyword3"
, because the text in the quotation marks would get separated as well.
So I need a function that separates the keywords, but doesn't separate the text in quotation marks. Is there a function that can do that? If not, is a regex the way to go?