mgm_03 Posted September 5, 2003 Share Posted September 5, 2003 I\'ve implemented FULLTEXT searching along with the score and it works great. If a user enters a keyword, the list of companies who match that keyword are returned. Is it possible to display the context in which the keyword was found? \"Our shop can do make prototypes from any kind of plastic\" Assuming the keyword searched was \"prototypes\", how could I get this sentence or (fragment of it) to be returned from the query? I guess this is like a real search engine. Any ideas? Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted September 7, 2003 Share Posted September 7, 2003 For each returned record you first have to determine which of keywords entered was found in the field, and at what position in the text. strpos() will give you this. Then starting at that position select several words either side and display with substr(). Quote Link to comment Share on other sites More sharing options...
Barand Posted September 7, 2003 Share Posted September 7, 2003 This\'ll give 10 words either side, or to begining and end of the sentence whichever is shorter. [php:1:e29276e025]<?php function context($keywords, $text) { $result = \'\'; $textLen = strlen($text); foreach ($keywords as $str) { if (($p = strpos($text,$str)) !== false) { $len = strlen($str); $spcount = 0; for ($i=$p-1; $i >= 0; $i--) { if ($text{$i} == \'.\') break; if ($text{$i}==\' \') { if (++$spcount > 10) break; } } $i++; $spcount = 0; $j1 = $p+$len; for ($j=$j1; $j < $textLen; $j++) { if ($text{$j} == \'.\') break; if ($text{$j}==\' \') { if (++$spcount > 10) break; } } return trim(substr($text,$i, $p - $i)) . \" <font color=\'red\'>$str</font> \" . trim(substr($text,$j1, $j - $j1)); } } return \'\'; } $text = \'By Lewis Carroll. Twas brillig and the slithy toves did gyre and gimble in the wabe. \' . \'All mimsy were the borogoves and the mome raths outgrabe.\'; $keywords = array(\'gyre\', \'jabberwock\'); echo context($keywords,$text); ?>[/php:1:e29276e025] hth Quote Link to comment Share on other sites More sharing options...
mgm_03 Posted September 8, 2003 Author Share Posted September 8, 2003 Hey man, Thanks! I had a feeling that strpos() was the function I needed but I had not implemented any code yet. Thanks for the code snippet, too. ...i\'ve got so much coding to do. fwiw...i found a good tutorial that simulates Google query results: http://zend.com/zend/tut//tutorial-brogdon2.php cheers. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.