Actually, now that I think about it some more, an alternate strategy could be to use the “REGEXP” operator instead of “LIKE”, which when combined with a concept known as “word boundaries”, could give you what you’re after. So the same 10 lines of code would now look like this:
if (!$q['sentence']) {
$s_array = explode(' ', $q['s']);
$q['search_terms'] = $s_array;
$search .= '((post_title REGEXP \'[[:<:]]' . $s_array[0] . '[[:>:]]\') OR (post_content REGEXP \'[[:<:]]' . $s_array[0] . '[[:>:]]\'))';
for ($i = 1; $i < count($s_array); $i = $i + 1) {
$search .= ' AND ((post_title REGEXP \'[[:<:]]' . $s_array[$i] . '[[:>:]]\') OR (post_content REGEXP \'[[:<:]]' . $s_array[$i] . '[[:>:]]\'))';
}
$search .= ' OR (post_title REGEXP \'[[:<:]]' . $q['s'] . '[[:>:]]\') OR (post_content REGEXP \'[[:<:]]' . $q['s'] . '[[:>:]]\')';
$search .= ')';
} else {
$search = ' AND ((post_title REGEXP \'[[:<:]]' . $q['s'] . '[[:>:]]\') OR (post_content REGEXP \'[[:<:]]' . $q['s'] . '[[:>:]]\'))';
}
For more info, see the regular expressions page in the MySQL manual. Just keep in mind that regular expressions are “more expensive”, so if your site is very busy, it may not be worth it.
P.S. My apologies to the board moderators who won’t be pleased with me copying lots of code here.