<?php
function highlightTerm(string $text, string $term): string {
if ($term === '') return htmlspecialchars($text, ENT_QUOTES);
$escapedText = htmlspecialchars($text, ENT_QUOTES);
$escapedTerm = htmlspecialchars($term, ENT_QUOTES);
$pattern = '/(' . preg_quote($escapedTerm, '/') . ')/iu';
return preg_replace($pattern, '<mark>$1</mark>', $escapedText);
}
echo highlightTerm("PHP is great, PHP is fun.", "php");
// <mark>PHP</mark> is great, <mark>PHP</mark> is fun.
Create a free account and build your private vault. Share publicly whenever you want.