Prev | Current Page 337 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"

The highlighted code that
follows is the one that breaks the search string into words and saves them into the accepted words and ignored
words arrays:
// Search string delimiters
$delimiters = ',.; ';
/* On the first call to strtok you supply the whole
search string and the list of delimiters.
It returns the first word of the string */
$word = strtok($searchString, $delimiters);
// Parse the string word by word until there are no more words
while ($word)
{
// Short words are added to the ignored_words list from $search_result
if (strlen($word) < FT_MIN_WORD_LEN)
$search_result['ignored_words'][] = $word;
else
$search_result['accepted_words'][] = $word;
// Get the next word of the search string
$word = strtok($delimiters);
}
Once this operation is done, we check again if we still have words to search for. This time, we check that the
accepted_words array is not empty. If it is, then we have nothing to search for, and so we return the empty
$search_result array:
// If there aren't any accepted words return the $search_result
if (count($search_result['accepted_words']) == 0)
return $search_result;
Now that we know we have at least one word to search for, it??™s time to prepare the search string that we??™ll send
to the database. As you know from the full-text search theory, when making All-Words searches, we use Boolean
searching, and we must prefix each searched word with a plus sign (+).


Pages:
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349