CodeIgniter Forums
substr full word? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: substr full word? (/showthread.php?tid=46387)



substr full word? - El Forum - 10-30-2011

[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