I have a search feature which is coded below and i was wondering whether it is possible to keep the original formatting. for example if i have the following piece of text, “Hello, I’m new to PHP” and in my search box i type “php” in lower case, in the results it will change the original upper case ‘PHP’ to a lower case ‘php’. is it possible to leave the string at its original state of PHP whether the user searches PhP or pHp etc.
here is the function containing the ‘str_ireplace’…
function boldText($text, $kword) {
return preg_replace('/($kword)/i', "<strong><font color='Red'>$kword</font></strong>", $text);
and here is where it is called…
echo "<td width = 130px><b>".boldText($info['company_name'], $kword) . "</b></td> ";
echo "<td width = 60px>".boldText($info['section_name'], $kword) . " </td>";
echo "<td width = 300px>".boldText($info['question'], $kword) . " </td>";
echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>";
Thanks
Use regular expressions. The modifier
istands for case insensitiveIn context of your function:
Single quoted strings do not parse variables that are inside!
So you need
'/('.$kword.')/i'or"/($kword)/i".Inside dobule quoted strings variables are parsed, but always be aware of strange side effects, with weird combinations.