- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
是否可以在不破坏原始外壳的情况下运行 str_ireplace?
例如:
$txt = "Hello How Are You";
$a = "are";
$h = "hello";
$txt = str_ireplace($a, "<span style='background-color:#EEEE00'>".$a."</span>", $txt);
$txt = str_ireplace($h, "<span style='background-color:#EEEE00'>".$h."</span>", $txt);
一切正常,但结果输出:
[hello] How [are] You
代替:
[Hello] How [Are] You
(方括号是彩色背景)
谢谢。
最佳答案
您可能正在寻找这个:
$txt = preg_replace("#\\b($a|$h)\\b#i",
"<span style='background-color:#EEEE00'>$1</span>", $txt);
... 或者,如果您想突出显示整个单词数组(也可以使用元字符):
$txt = 'Hi! How are you doing? Have some stars: * * *!';
$array_of_words = array('Hi!', 'stars', '*');
$pattern = '#(?<=^|\W)('
. implode('|', array_map('preg_quote', $array_of_words))
. ')(?=$|\W)#i';
echo preg_replace($pattern,
"<span style='background-color:#EEEE00'>$1</span>", $txt);
关于php str_ireplace 不丢失大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740116/
是否可以在不破坏原始外壳的情况下运行 str_ireplace? 例如: $txt = "Hello How Are You"; $a = "are"; $h = "hello"; $txt = st
如何使用 str_ireplace(或类似的东西)来替换一些文本以进行格式化,然后用相同的大写字母返回它? 例子: $original="The quick red fox jumps over th
我想替换包含特定单词的字符串,无论是大写、小写还是混合(不区分大小写),我尝试使用 str_ireplace 函数,但目标字符串根本没有替换 $fileName = "banner_2012.jpg"
我有一个编码如下的搜索功能,我想知道是否可以保留原始格式。例如,如果我有以下文本,“你好,我是 PHP 新手”,在我的搜索框中输入小写的“php”,在结果中它会将原来的大写“PHP”更改为小写'php
我需要在俄语文本中突出显示搜索结果,但是当查询有不同的大小写时 str_ireplace() 就不起作用。我已经尝试了我在 manual 中遇到的所有问题,但没有任何效果。这是我尝试过的: $qu
我需要从 MySQL 表中获取一个值,然后将其替换为其他值。 我有一个查找和替换表,其中包含三列:ID、查找、替换。我想循环遍历该表的每一行,然后在另一个表中搜索查找值并将其更改为替换。 例如: ID
当我运行脚本时,它在数据库中存储空白。我哪里错了。下面是 php 脚本: PayBill 1234560000007031 20140227082020
注意:我认为我知道的可能是错误的,所以请修正我的知识:) I just answered a question about UTF-8 and PHP. 我建议使用 str_ireplace('Вол
我已阅读 this post讨论将 html 中断标记转换为 php 中的新行。其他人说这对他们有用,但我发生了一些奇怪的事情。 这是我使用的代码: $breaks = array("", "", "
我是一名优秀的程序员,十分优秀!