What is the best method to highlight words when retrieving them from a database in PHP?

When retrieving words from a database in PHP and you want to highlight specific words within the retrieved text, one common method is to use the `str_replace()` function to replace the words with HTML tags for highlighting. You can wrap the words with `<span>` tags and apply a CSS class for styling. This allows you to visually emphasize the words when displaying them on a webpage.

&lt;?php
// Retrieve text from the database
$text = &quot;This is a sample text with some words to highlight.&quot;;

// Words to highlight
$wordsToHighlight = array(&quot;sample&quot;, &quot;words&quot;);

// Loop through each word to highlight
foreach($wordsToHighlight as $word) {
    $text = str_replace($word, &quot;&lt;span class=&#039;highlight&#039;&gt;$word&lt;/span&gt;&quot;, $text);
}

// Display the highlighted text
echo $text;
?&gt;