What is the purpose of the highlightPhrase function in the provided PHP code?
The purpose of the highlightPhrase function in the provided PHP code is to search for a specific phrase within a given text and wrap it in an HTML tag for highlighting purposes. However, the current implementation of the function is not working correctly because it is not handling case sensitivity properly. To solve this issue, we need to modify the function to perform a case-insensitive search for the phrase within the text.
function highlightPhrase($text, $phrase) {
$highlightedText = preg_replace('/\b' . preg_quote($phrase, '/') . '\b/i', '<span style="background-color: yellow;">$0</span>', $text);
return $highlightedText;
}
// Example usage
$text = "This is a sample text to highlight a specific phrase.";
$phrase = "sample";
$highlightedText = highlightPhrase($text, $phrase);
echo $highlightedText;
Related Questions
- What potential pitfalls can arise when using PDO prepared statements for INSERT ON DUPLICATE KEY UPDATE in PHP?
- What specific steps should be taken to properly format and indent PHP code for better readability and debugging?
- Why is it important to subtract 1 from the date value received from date() when using it as an array index in PHP?