Forum Moderators: coopster

Message Too Old, No Replies

Producing snippets of text

Like G does in SERPs

         

ThomasB

12:12 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I currently have the following code:

$select = "SELECT * FROM text WHERE MATCH(content) AGAINST ('".$keyword."') LIMIT 0,50";

$query = mysql_query($select);

while ($info = mysql_fetch_object($query)) {
$results[] = $info->content;
}

foreach($results as $result) {

echo $result."<br> ";

};

This works pretty well. But I'd like to have snipptes only.
ie
keyword = "new books"
content = "I recently read a new article about information that was published in books a long time ago."
result = "... read a new article about ... published in books a long ..."

Any ideas how to perform this task?

Lord Majestic

12:31 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about this: use regular expressions to "highlight" matches (new, books) with <b>'s and then identify positions of <b>'s and allow up to X characters before each match with some additional constraints to avoid having too much text.

Another way to do it would be split whole text into words, then walk word by word checking if its a match in which case take last and next X words for snippet's purposes.

Can't say whether its the ideal way to do it (I am yet to implement it for my search engine) but it should work well for 10-20 displayed matches :)

jatar_k

2:18 am on Feb 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or maybe some combination of strpos, count_chars and explode on spaces

which would essentially give you the word position in the string and then allow you to output words before and after your given position/word.