OK here is the revised code (with more help from Zbings!). The only problem is that it works like this now:
Say text being searched is: "ABC is cool but AbC is cooler!";
Text we're searching for is "abc"
Result ends up being: "abc is cool but abc is cooler!"
The original case of the words don't stay, instead they all become lowercase...
function isolate_results($block, $pre_len, $post_len)
{
//lowercase copies
$lblock = strtolower($block);
$lkeywords = strtolower($this->keywords);
//first occurence of keyword(s)
$pos = strpos($lblock, $lkeywords, 1);
//length of keyword
$len = strlen($this->keywords);
//check that the keyword was found
if($pos == 0)
{
$result = substr($block, 0, 200) . '...';
}
else
{
//check if the pre_len is less than 0
if(($pos - $pre_len) < 0)
{
$pre_len = 0;
//prefix
$prefix = '';
}
else
{
$pre_len = $pre_len;
//prefix
$prefix = '...';
}
//make sure post_len isnt bigger than the block
if(($pos + $post_len) > strlen($block))
{
$post_len = strlen($block) - $pre_len;
}
else
{
$post_len = $post_len;
}
//suffix string
if($post_len + $pre_len == strlen($block))
{
$suffix = '';
}
else
{
$suffix = '...';
}
//replace keywords with
$replace = '<strong>' . $this->keywords . '</strong>';
//body to return
$body = eregi_replace($this->keywords, $replace, $block);
//Start and end calculations
$start = $pos - $pre_len;
$end = $pre_len + $len + $post_len;
//setup result
$result = $prefix; //attach prefix
$result.= substr($body, $start, $end); //point to result
$result.= $suffix; //attach suffix
}
return $result;
}