Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使字符串的特定部分加粗_Php_Arrays_String - Fatal编程技术网

Php 使字符串的特定部分加粗

Php 使字符串的特定部分加粗,php,arrays,string,Php,Arrays,String,我有一个类似的数组 $array[]="This is a test"; $array[]="This is a TEST"; $array[]="TeSt this"; 我需要将字符串“test”设置为boldlike $array[]="This is a <b>test</b>"; $array[]="This is a <b>TEST</b>"; $array[]="<b>TeSt</b> this";

我有一个类似的数组

 $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";
$array[]=“这是一个测试”;
$array[]=“这是一个测试”;
$array[]=“测试此项”;
我试过使用
str_replace()
,但它区分大小写

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

试试看。
str\u replace的不区分大小写版本

stru-ireplace(“test”、“test”和$array);
str_ireplace(“TSET”、“TEST”、“数组”);

如果要查找模式而不是像“test”这样的固定字符串,请查看正则表达式和
preg\u replace

$str = preg_replace("#(test|otherword)#i", "<b>$1</b>", $str);
$str=preg#u replace(“#(test | otherword)#i”、“$1”、$str);
有关正则表达式的更多信息:


编辑:在正则表达式后添加“i”以删除大小写敏感性。

您可以使用我在下面编写的函数:

function wrap_text_with_tags( $haystack, $needle , $beginning_tag, $end_tag ) {
    $needle_start = stripos($haystack, $needle);
    $needle_end = $needle_start + strlen($needle);
    $return_string = substr($haystack, 0, $needle_start) . $beginning_tag . $needle . $end_tag . substr($haystack, $needle_end);
    return $return_string;
}
所以你可以这样称呼它:

$original_string = 'Writing PHP code can be fun!';
$return_string = wrap_text_with_tags( $original_string , 'PHP' , "<strong>" ,"</strong>");
$original_string='编写PHP代码会很有趣!';
$return\u string=用标签($original\u string,'PHP',“”,“”)包装文本;
返回时,字符串将如下所示:

$original_string = 'Writing PHP code can be fun!';
$return_string = wrap_text_with_tags( $original_string , 'PHP' , "<strong>" ,"</strong>");
原始字符串

编写PHP代码会很有趣

修改结果

编写PHP代码会很有趣


此函数仅适用于字符串的第一个实例。

您可以使用
array\u 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');
函数my_str_替换(&$item){
$item=preg_replace(“/test/i”,“$0”,“$item”);
}
$array[]=“这是一个测试”;
$array[]=“这是一个测试”;
$array[]=“测试此项”;
数组_walk($array,“my_str_replace”);
编辑:基于John WH Smith的评论


您只需使用
$array=preg_replace(“/test/i”、“$0”、$array”)
哪一个会起到神奇的作用呢

@shijin在发帖前有一个问题提到你的确切要求,你说**我用str_replace()试过了,但它是区分大小写的,请帮助我**这就是为什么我被提供str_ireplace
str_ireplace(“test”,“test”,“string”)会将“test”改为粗体,您可以将此值存储在其他位置,并保持原始文本不变。这是一个有效的答案,无论得到多少次反对票,都不会被删除。编辑后,现在很清楚(不是很清楚)您想用
TeSt
替换
TeSt
。您应该使用建议的正则表达式解决方案抱歉这样暗示。事实上,我后来意识到我应该投反对票。我应该理解,在替换文本时,我们应该保持原样,从您的示例中可以清楚地看出。再次感谢您的帮助:)我强烈建议您在这里进一步阅读PHP参考(
&$item
)。为什么不从这里开始:(我不确定这里是否需要
array\u walk
,因为
preg\u replace
可以同时处理字符串和数组(
mixed
参数:))