Question

I have a search input and when the script shows the result want I to bold and underline the keyword. So I have this script:

$showkey = str_replace(ucfirst($_POST['station']), '<b><u>'.htmlentities(ucfirst($_POST["station"]), ENT_QUOTES).'</u></b>', $founds["fullname"][$i]);
$showkey = str_replace(lcfirst($_POST["station"]), '<b><u>'.htmlentities(lcfirst($_POST["station"]), ENT_QUOTES).'</u></b>', $founds["fullname"][$i]);

So, for example: If $_POST["station"] is 'l' I want every l and L is bold and underlined. Thats now not working, only the small 'l' is bold and underlined. When the script is this:

$showkey = str_replace(lcfirst($_POST["station"]), '<b><u>'.htmlentities(lcfirst($_POST["station"]), ENT_QUOTES).'</u></b>', $founds["fullname"][$i])
$showkey = str_replace(ucfirst($_POST['station']), '<b><u>'.htmlentities(ucfirst($_POST["station"]), ENT_QUOTES).'</u></b>', $founds["fullname"][$i]);

Now only the 'L' is bold and underlined. But I want the L and l both bold and underlined.

How can I do that?

Thanks!

Was it helpful?

Solution

You should be able to solve this using regular expressions:

preg_replace('/('. $_POST['station'] .')/i', '<b><u>$1</u></b>', $text);

But take care to escape special characters in $_POST['station'] if you use it this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top