I want to match an array against a text string where words matching with the array keys will be replaced by a span tag with a title with the matching array value.
I can
You can run this replace in loop:
$txt = 'Hi! How are you doing? Have some stars: * * *!';
$glossary = array(
'Hi!' => 'title 1',
'stars' => 'title 2',
'*' => 'title 3'
);
$result = $txt;
foreach ($glossary as $keyword => $title) {
$pattern = '#(?<=^|\W)('.preg_quote($keyword).')(?=$|\W)#i';
$result = preg_replace($pattern, "$1", $result);
}
echo $result;