What are the potential pitfalls of using str_ireplace for highlighting keywords in a CSV import in PHP?

Using str_ireplace for highlighting keywords in a CSV import in PHP may lead to unintended replacements in the data if the keywords are found within other words. To avoid this issue, a more precise approach using regular expressions with word boundaries should be used to ensure only whole words are replaced.

// Example code snippet using regular expressions with word boundaries to highlight keywords in a CSV import
$csvData = "apple,banana,orange,grape\napple pie,banana bread,orange juice,grapefruit";
$keywords = ['apple', 'banana', 'orange', 'grape'];

foreach ($keywords as $keyword) {
    $csvData = preg_replace("/\b($keyword)\b/i", "<strong>$1</strong>", $csvData);
}

echo $csvData;