<?php
use warnings;
$glossary = array(
    'word1' => '<span title="Explanation for Word 1">word1</span>',
    'word2' => '<span title="Explanation for Word 2">word2</span>',
    'word3' => '<span title="Explanation for Word 3">word3</span>'
);

$text = "This is a text about Word1. We also talk about word2 and WORD3. Also woRd3, word2 and WORD1";

$pttrns = array_map(function ($val) {return "/" . preg_quote($val) . "/i"; },array_keys($glossary));
$res = preg_replace_callback($pttrns, function ($m) use($glossary){
	return str_replace(strtolower($m[0]), $m[0], $glossary[strtolower($m[0])]);
}, $text);
echo $res;