Highlight parts within a string [SOLVED]
Posted: Fri Oct 27, 2006 8:35 pm
The following function was pulled from the PHP.net web site:
http://www.php.net/manual/en/function.s ... .php#33425
It works almost perfectly. The problem is if $var = "free software" and $x is the following text:
the the function will only highlight free software, and not each word free and/or software.
I know that I need to break $var into pieces and then highlight them individually, but I dont know how to do it. Can anyone show me how?
Thank you in advance!
http://www.php.net/manual/en/function.s ... .php#33425
Code: Select all
function highlight($x,$var) {
if ($var != "") {
$xtemp = "";
$i=0;
while($i<strlen($x)){
if((($i + strlen($var)) <= strlen($x)) && (strcasecmp($var, substr($x, $i, strlen($var))) == 0)) {
$xtemp .= "<b>" . substr($x, $i , strlen($var)) . "</b>";
$i += strlen($var);
}
else {
$xtemp .= $x{$i};
$i++;
}
}
$x = $xtemp;
}
return $x;
}Code: Select all
Script Installation, trouble installing web scripts, perl scripts, Downloads, Free Software Download, Free Ware Download, Free Webmaster Tools - Apply Tools FREE WEBMASTER DOWNLOADS | FREE SOFTWARE DOWNLOAD | FREE WARE DOWNLOAD | FREE WEBMASTER TOOLS Your Ad Here Search FeaturesI know that I need to break $var into pieces and then highlight them individually, but I dont know how to do it. Can anyone show me how?
Thank you in advance!