'How to highlight the whole word in bold [closed]

There is a suggestion: teg are divided into two categories.

code:

echo preg_replace("/($word)/i","<b>$1</b>", htmlspecialchars($words[i])."<br>";

If a word, such as a teg, only highlights it in bold. How to highlight an entire word, using the code above, highlights only part of the category

php


Solution 1:[1]

From the given context, you can perform a match of the word you are looking for with matching optional non-space characters before or after it surrounded by a word boundary like below:

<?php

$str = 'HTML-document';

$word = 'HTML';

echo preg_replace('/\b[^\s]*'.preg_quote($word).'[^\s]*\b/',"<b>$0</b>", $str);                          

Online Demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1