I would use regular expressions:
$text = preg_replace('~(' . preg_quote($search, '~') . ')~i', '<span>$1</span>', $text);
There are other ways, for example, soulmerge ( str_ireplace()):
$text = str_ireplace($search, '<span>' . $search . '</span>', $text);
source
share