gpt4 book ai didi

php - 使用 str_ireplace 时保留原始字符串格式

转载 作者:可可西里 更新时间:2023-11-01 00:48:30 26 4
gpt4 key购买 nike

我有一个编码如下的搜索功能,我想知道是否可以保留原始格式。例如,如果我有以下文本,“你好,我是 PHP 新手”,在我的搜索框中输入小写的“php”,在结果中它会将原来的大写“PHP”更改为小写'php'。无论用户搜索 PhP 还是 pHp 等,是否可以将字符串保留在 PHP 的原始状态?

这里是包含“str_ireplace”的函数...

function boldText($text, $kword) {
return preg_replace('/($kword)/i', "<strong><font color='Red'>$kword</font></strong>", $text);

这里就是它的名字...

 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>";

谢谢

最佳答案

使用regular expressions .修饰符 i 代表大小写不敏感

echo preg_replace('/(php)/i', "<strong><font color='Red'>$1</font></strong>", 'I am new to pHp')
// returns "I am new to <strong><font color='Red'>pHp</font></strong>"

在您的职能范围内:

function boldText($text, $kword) {
    return preg_replace('/('.$kword.')/i', "<strong><font color='Red'>$1</font></strong>", $text);
}

单引号字符串不解析里面的变量!

所以你需要 '/('.$kword.')/i'"/($kword)/i"

内部双引号字符串变量被解析,但始终注意奇怪的副作用,以及奇怪的组合。

关于php - 使用 str_ireplace 时保留原始字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12604584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com