Welcome Guest, Not a member yet? Register   Sign In
substr full word?
#1

[eluser]Unknown[/eluser]
I am using the below code to highlight a keyword searched for in my database.

Code:
$word = 'invest';
$characters_before="80";
$characters_after="80";
function supertruncate($text, $word, $characters_before, $characters_after){
                  $pos = strpos($text, $word);
$start = $characters_before < $pos ? $pos - $characters_before : 0;
$len = $pos + strlen($word) + $characters_after - $start;
                   $text =  substr($text, $start, $len);
                  
                  return  str_ireplace($word, '<span class="highlight">' . $word . '</span>', $text);
}

It truncates the lengthy and adds characters to the left and right of the searched keyword.

How can I, using the above code, ensure that it truncates after whole words and not characters???

Finding it tricky




Theme © iAndrew 2016 - Forum software by © MyBB