Make bold specific part of string

arrays, php, string

Solution

You can use `array_walk` PHP function to replace the string value within an array. Check below code

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');

EDIT: Based on John WH Smith's comment

You can simply use `$array = preg_replace("/test/i", '<b>$0</b>', $array);` which would do the magic

Problem

I have an array like ``` $array[]="This is a test"; $array[]="This is a TEST"; $array[]="TeSt this"; ``` I need to make the string 'test' as `bold` like ``` $array[]="This is a <b>test</b>"; $array[]="This is a <b>TEST</b>"; $array[]="<b>TeSt</b> this"; ``` I have tried with `str_replace()` but it is case sensitive, Note: I need to make the given string bold and keep as it is.

Original source

Related problems