Recommendations for displaying search results with corresponding text fragments from the actual result

I have a decent, lightweight search engine running on one of my sites using full-text MySQL and php indexes to analyze the results. The work is great, but I would like to suggest more "google-like" results with fragments of text from the results and highlighted words. Looking for a php based solution. Any recommendations?

+4
source share
4 answers

Finding the actual database is fine until you want to add exciting features like the one above. In my experience, it’s better to create a special search table with keywords and page IDs / URLs / etc. Then fill this table every n hours with content. During this group, you can add snippets for each document for each keyword.

An alternative could be a quick hack:

<?php $text = 'This is an example text page with content. It could be red, green or blue.'; $keyword = 'red'; $size = 5; // size of snippet either side of keyword $snippet = '...'.substr($text, strpos($text, $keyword) - $size, strpos($text, $keyword) + sizeof($keyword) + $size).'...'; $snippet = str_replace($keyword, '<strong>'.$keyword.'</strong>', $snippet); echo $snippet; ?> 
+5
source

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 .

+3
source

use preg_replace() (or a similar function) and replace the search string with the selected text. eg.

 $highlighted_text = preg_replace("/$search/", "<span class='highlighted'>$search</span>", $full_text); 
+1
source

On a larger site, I would think that using javascript, something like jquery would be the way

-1
source

All Articles