Jump to content

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!

Link to comment
https://forums.phpfreaks.com/topic/985-fulltext-would-like-to-get-context/
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().

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.