str_ireplace() 并保持大小写

发布于 2024-09-14 14:54:20 字数 339 浏览 4 评论 0原文

如何使用 str_ireplace (或类似的东西)替换某些文本以进行格式化,然后使用相同的大写字母返回它?

示例:

$original="The quick red fox jumps over the lazy brown dog.";
$find="thE";

print str_ireplace($find,'<b>'.$find.'</b>',$original);

这将输出: 敏捷的红狐跳过了懒惰的棕色狗。

我希望它保留原始大小写并仅应用格式,在本例中为粗体文本。

谢谢。

How can I use str_ireplace (or something similar) to replace some text for formatting and then return it with the same caps?

Example:

$original="The quick red fox jumps over the lazy brown dog.";
$find="thE";

print str_ireplace($find,'<b>'.$find.'</b>',$original);

That will output:
thE quick red fox jumps over the lazy brown dog.

I want it to keep the original case and only apply the formatting, in this example, bold text.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我恋#小黄人 2024-09-21 14:54:20
$original = "The quick red fox jumps over the lazy brown dog.";
$new = preg_replace("/the/i", "<b>\$0</b>", $original);

给出“敏捷的红狐跳过懒惰的棕色狗。”如果要匹配特定单词,可以添加单词边界:preg_replace('/\bthe\b/i', ...

如果要参数化替换,可以使用preg_quote

 preg_replace('/\b' . preg_quote($word, "/") . '\b/i', "<b>\$0</b>", $original);
$original = "The quick red fox jumps over the lazy brown dog.";
$new = preg_replace("/the/i", "<b>\$0</b>", $original);

gives "The quick red fox jumps over the lazy brown dog." If you want to match specific words, you can add word boundaries: preg_replace('/\bthe\b/i', ....

If you want to parameterize the replacement, you can use preg_quote:

 preg_replace('/\b' . preg_quote($word, "/") . '\b/i', "<b>\$0</b>", $original);
淡淡绿茶香 2024-09-21 14:54:20

要么替换为确切的单词,要么使用 preg_replace:

preg_replace('/(The)/i', "<strong>$1</strong>", $original);

Either replace with the exact word, or use preg_replace:

preg_replace('/(The)/i', "<strong>$1</strong>", $original);
迟到的我 2024-09-21 14:54:20

我编写了一个替代解决方案(使用纯字符串替换的函数)来完成这项工作,它允许您轻松设置前缀和后缀(用于突出显示):

function replace_keep_case($st1, $find1) {

/// set prefix and suffix here
$prefix="<span style='color:red;'><b>";
$suffix="</b></span>";

$index=0;
$resultstring="";
   while ($index < strlen($st1)) {
      if (strtoupper(substr($st1, $index, strlen($find1)))==strtoupper($find1)) {
        $resultstring.=$prefix.substr($st1, $index, strlen($find1)). $suffix;
        $index=$index+strlen($find1); 
      } else {
        $resultstring.=substr($st1, $index, 1);
        $index++;
      }
    }
   return $resultstring; 
}

例如:

$text="CHICHiOn Ichiro's Malt & Grain (Limited Edition) 700ml 48% CONDITION";

$findstring="ion";

echo replace_keep_case($text, $findstring);

I have written an alternative solution (a function which uses pure string replacement) to do the job, which allows you to easily set the prefix and suffix (for highlighting):

function replace_keep_case($st1, $find1) {

/// set prefix and suffix here
$prefix="<span style='color:red;'><b>";
$suffix="</b></span>";

$index=0;
$resultstring="";
   while ($index < strlen($st1)) {
      if (strtoupper(substr($st1, $index, strlen($find1)))==strtoupper($find1)) {
        $resultstring.=$prefix.substr($st1, $index, strlen($find1)). $suffix;
        $index=$index+strlen($find1); 
      } else {
        $resultstring.=substr($st1, $index, 1);
        $index++;
      }
    }
   return $resultstring; 
}

For example:

$text="CHICHiOn Ichiro's Malt & Grain (Limited Edition) 700ml 48% CONDITION";

$findstring="ion";

echo replace_keep_case($text, $findstring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文