php str_replace letter to html


php str_replace letter to html

我有一个搜索输入,当脚本显示结果时,我希望我加粗并为关键字加下划线。所以我有这个脚本:

$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]);

因此,例如:如果 $_POST["station"] 是"l",我希望每个 l 和 L 都加粗和下划线。现在不起作用,只有小的"l"是粗体和下划线。当脚本是这样的:

$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]);

现在只有"L"是粗体和下划线。但我希望 L 和 l 都加粗和下划线。

我该怎么做?

谢谢!

你应该能够使用正则表达式来解决这个问题:

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

但是,如果您以这种方式使用它,请注意转义 $_POST['station'] 中的特殊字符。

相关文章: