I'm setting up a word search using PHP's explode(), and counting how many spaces from query and how many words in the query.
For example, my user search Hello world, good morning (query from user, maybe more words)
and I get:
- hello
- world
- good
- morning
I want to show the unique sets of words as arrays like:
- ['hello world good morning']
- ['hello world good', 'world good morning']
- ['hello world', 'good morning', 'world good']
- ['hello', 'world', 'good', 'morning']
For no 1 and 4 I can solve it, but no 2 and 3 it's so hard to thing.
$oriSearch = 'Hello world, good morning'; $search_query = trim(strtolower($oriSearch)); $search_query = preg_replace_callback('#([\W_]+)#', function() { return ' '; }, $search_query); $totalSpace = substr_count($search_query, ' '); $totalWord = ceil($totalSpace+1); if($totalSpace > 0) { $wordPlode = explode(' ', $search_query); $wordQuery = array(); for($i=1;$i
Источник: https://stackoverflow.com/questions/556 ... word-count