below is an extract from one of my old search scripts, it takes the search words, finds there position in the returned text, then creates an extract of the words and what surrounds them.
<?php
foreach ($search_words_cleaned as $search_words_cleaned_match) {
$findme_l = strlen($search_words_cleaned_match);
if ($option == 'fuzzy') {
$preg_search_words = "/" . $search_words_cleaned_match . "/i";
} else {
$preg_search_words = "/\b" . $search_words_cleaned_match . "\b/i";
}
@$title = preg_replace($preg_search_words, '<strong>\0</strong>', $title) or
die('Error, try again'); //parsed chars in search ****ing up preg
$var= preg_match_all($preg_search_words,$row['body'],$pos_array_x,PREG_OFFSET_CAPTURE);
// $var= preg_match_all('/'.$search_words_cleaned_match.'/i',$row['body'],$pos_array_x, PREG_OFFSET_CAPTURE);
foreach($pos_array_x as $key=>$var){
foreach($var as $key2=>$var2){
$pos_array[]= $var2['1'];
} //end foreach var2
} //end foreach var
} //end foreach ($search_words_cleaned as $search_words_cleaned_match)
// print_r($pos_array); //sorted by keyword
if( !empty($pos_array)){
sort($pos_array); //sorted by pos
$pos_array_count= count($pos_array);
$pos_array_out= array("$pos_array[0]"); //put the first one in
foreach($pos_array as $pos_array_check){
for($zz= 0;$zz < $pos_array_count and count($pos_array_out) < 2;++$zz){ // 2 is nunber of extracts
if(($pos_array_check - $pos_array[$zz]) > 70){
$pos_array_out[]= $pos_array_check;
break;
}
}
}
}else{
$pos_array_out= array("$pos_array[0]"); //put the first one in if needed
}
// echo '<br />^';
// print_r($pos_array_out);
foreach($pos_array_out as $pos_e){
if($pos_e <= 70){
$match_pos= 0;
}else{
$match_pos= $pos_e - 70;
}
if($findme_l + 140 < $row_body_len){
$offset= $findme_l + 140;
}else{
$offset= $row_body_len - $findme_l;
}
if($row_body_len > 140){
$extract= substr($row['body'],$match_pos,$offset);
$ext_len= strlen($extract);
if($row['id'] == 'p'){
$start= strpos($extract,"\n");
// echo $extract;
$last= strrpos($extract,"\n");
}else{
$start= strpos($extract,' ');
$last= strrpos($extract,' ');
}
$end= ($ext_len - ($ext_len - $last)) - ($start);
if($match_pos != 0){
$extract= trim(substr($extract,$start,$end));
}else{
$extract= trim(substr($extract,0,$end));
$ext= 'start';
}
}else{ // less than 140
$extract= $row['body'];
}
foreach($search_words_cleaned as $search_words_cleaned_match){
if($option == 'fuzzy'){
$preg_search_words= "/" . $search_words_cleaned_match . "/i";
}else{
$preg_search_words= "/\b" . $search_words_cleaned_match . "\b/i";
}
$extract= preg_replace($preg_search_words,'<strong>\0</strong>',$extract);
}
$extract_array[$pos_e]= $extract;
}
?>
it may be such a mess its unusable, feel free to ignore :-)