Shijin TR
Shijin TR

Reputation: 7768

Make bold specific part of string

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.

Upvotes: 5

Views: 13834

Answers (7)

mickmackusa
mickmackusa

Reputation: 48001

I find using a preg_replace() call to be the most appropriate tool for this task because:

  • it can affect all elements in the array without writing a loop,
  • it can replace more than one substring within a string,
  • adding a case-insensitive flag (i) is an easy and intuitive adjustment,
  • adding word boundaries (/b) on either side of the "needle" word will ensure that only whole words are replaced
  • when replacing the fullstring match, no parentheses / capture groups are necessary.

Code: (Demo)

$array = [
    "This is a test",
    "This is a TEST",
    "Test this testy contest protest test!",
    "TeSt this",
];

var_export(
    preg_replace('/\btest\b/i', '<b>$0</b>', $array)
);

Output:

array (
  0 => 'This is a <b>test</b>',
  1 => 'This is a <b>TEST</b>',
  2 => '<b>Test</b> this testy contest protest <b>test</b>!',
  3 => '<b>TeSt</b> this',
)

Upvotes: 0

MRVN
MRVN

Reputation: 1

This is my solution. It also keeps all uppercase letters uppercase and all lowercase letters lowercase.

    function wrapTextWithTags( $haystack, $needle , $tag ): string
    {
        $lowerHaystack = strtolower($haystack);
        $lowerNeedle = strtolower($needle);

        $start = stripos($lowerHaystack, $lowerNeedle);
        $length = strlen($needle);

        $textPart = substr($haystack, $start, $length);
        $boldPart = "<" . $tag . ">" . $textPart . "</" . $tag . ">";

        return str_replace($textPart, $boldPart, $haystack);
    }

Upvotes: 0

Lepanto
Lepanto

Reputation: 1413

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

Upvotes: 6

bdb.jack
bdb.jack

Reputation: 147

You can use a function like the one I wrote below:

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;
}

So you'd be able to call it as follows:

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

When returned the strings will look as follows:

Original String

Writing PHP code can be fun!

Modified Result

Writing PHP code can be fun!

This function only works on the FIRST instance of a string.

Upvotes: 1

John WH Smith
John WH Smith

Reputation: 2773

If you're looking for patterns instead of fixed strings like "test", have a look at REGEXes and preg_replace :

$str = preg_replace("#(test|otherword)#i", "<b>$1</b>", $str);

More about REGEXes :

Edit : added "i" after the REGEX to remove case sensitivity.

Upvotes: 5

user3427568
user3427568

Reputation:

Try this Using str_ireplace

str_ireplace("test", "<b>test</b>", $array);
str_ireplace("TSET", "<b>TEST</b>", $array);

Upvotes: -1

mesutozer
mesutozer

Reputation: 2859

Try str_ireplace. Case insensitive version of str_replace

Upvotes: -1

Related Questions