Hello guys I'm making a search engine script. There is one part in the script that I have problem with...
This function is supposed to make the description that will be displayed bolded if it's the same with the search term....
PHP Code:
function make_bolded($string,$term){
$words = explode(" ",$string);
$bolded_string = "";
$sterms = explode(" ",$term);
for($i=0;$i<count($words);$i++){
$w = $words[$i];
$lowword = strtolower($w);
for($x=0;$x<count($sterms);$x++){
$lowsterm = strtolower($sterms[$x]);
switch($lowword){
case $lowsterm : $w = "<b>" . $words[$i] . "</b>";
break;
case "{$lowsterm}'" : $w = "<b>" . $words[$i] . "</b>";
break;
case "{$lowsterm}'s" : $w = "<b>" . $words[$i] . "</b>";
break;
case "{$lowsterm}," : $w = "<b>" . $words[$i] . "</b>";
break;
case "{$lowsterm}." : $w = "<b>" . $words[$i] . "</b>";
break;
case "{$lowsterm};" : $w = "<b>" . $words[$i] . "</b>";
break;
}
}
$bolded_string .= " " . $w . " ";
}
return $bolded_string;
}
for example...if you do $boldedhello = make_bolded("hello my","hello") , this function's supposed to make the "hello" in the string "hello my" bolded...so $boldedhello should contain the string "<b>hello</b> my"....
The problem is...that it only change the words into bolded if it's not the first or last word in the string...and I wonder what's wrong with my code that it does such thing.
For example
$bolded = make_bolded("what the heck","what");
will make $bolded contain "what the heck" even though it's supposed to be "<b>what</b> the heck"...
But if you do
$bolded = make_bolded("what what the heck","what");
It will change the second what into bolded like this:
"what <b>what</b> the heck"....
Can any of you help...thank you very much.....