For MySQL, the best choice would be to first split your query words, clear your values, and then combine everything back into a nice regex.
You can use the <strong> tag to highlight your results. Its use will be semantic, since you put strong on the element.
// Done ONCE per page load: $search = "Hello World"; //Remove the quotes and stop words $search = str_ireplace(array('"', 'and', 'or'), array('', '', ''), $search); // Get the words array $words = explode(' ', $search); // Clean the array, remove duplicates, etc. function remove_empty_values($value) { return trim($value) != ''; } function regex_escape(&$value) { $value = preg_quote($value, '/'); } $words = array_filter($words, 'remove_empty_values'); $words = array_unique($words); array_walk($words, 'regex_escape'); $regex = '/(' . implode('|', $words) . ')/gi'; // Done FOR EACH result $result = "Something something hello there yes world fun nice"; $highlighted = preg_replace($regex, '<strong>$0</strong>', $result);
If you use PostgreSQL, you can simply use the built-in ts_headline as described in the documentation .
source share