Jump to content

Archived

This topic is now archived and is closed to further replies.

mgm_03

FULLTEXT - would like to get context

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.