gpt4 book ai didi

php - 使字符串的特定部分加粗

转载 作者:行者123 更新时间:2023-12-03 23:00:54 26 4
gpt4 key购买 nike

我有一个类似的数组

 $array[]="This is a test";
$array[]="This is a TEST";
$array[]="TeSt this";

我需要将字符串 'test' 设置为 bold like

 $array[]="This is a <b>test</b>";
$array[]="This is a <b>TEST</b>";
$array[]="<b>TeSt</b> this";

我试过 str_replace() 但它区分大小写,

注意:我需要将给定的字符串加粗并保持原样。

最佳答案

您可以使用 array_walk PHP 函数用于替换数组中的字符串值。检查下面的代码

function my_str_replace(&$item){
$item = preg_replace("/test/i", '<b>$0</b>', $item);
}

$array[]="This is a test";
$array[]="This is a TEST";
$array[]="TeSt this";

array_walk($array, 'my_str_replace');

编辑:基于 John WH Smith 的评论

您可以简单地使用 $array = preg_replace("/test/i", '<b>$0</b>', $array);这会变魔术

关于php - 使字符串的特定部分加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22730461/

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