Codice PHP:
function snippet $str $keyword $words )
{
    
$left_str '' $right_str '' ;
 
    
$ts explode $keyword $str ) ;

    
$left_words array_reverse explode ' ' $ts[0] ) ) ;
    
$right_words explode ' ' $ts[1] ) ;
    
    for ( 
$i $i <= $words $i++ )
    {
        
$left_str .= $left_words[$words-$i] . ' ' ;
        
$right_str .= $right_words[$i] . ' ' ;
    }
    
    return 
'... ' $left_str '[b]' $keyword '[/b] ' $right_str ' ...' ;
}

echo 
'stringa_completa: ' $str '
;

echo 
'stringa_trattata:' .  snippet $str$keyword) ; 
credo si possa fare anche utilizzando le sole espressioni regolari... ma con quelle non riesco ancora a fare cose del genere