Well, the only workaround I have found is to surround every single
word with a <span> element. This is not a fix, but it works for me.
The function below will wrap your HTML string with span tags and
return the new HTML string. You should only pass the body, it will not
work with doctype and the head tags.
function WrapSpans($htmlString) {
//Split on all space characters
$htmlWords = preg_split('/[[:space:]]/', $htmlString);
$newHtmlString = "";
$insideHtmlTag = false;
//Loop through all the words
foreach ($htmlWords as $word) {
if($word != ' ' && $word != '') {
//Both < and > are in the word
if(strpos($word, "<") !== false && strpos($word, ">") !==
false) {
//Word looks like: <tag>word</tag><otherTag
//so we are inside a tag
if(strrpos($word, "<") > strrpos($word, ">")) {
$insideHtmlTag = true;
}
//Word is a normal tag: <tag>
else {
$insideHtmlTag = false;
}
$newHtmlString .= $word.' ';
continue;
}
//Word contains > character, but not <
if(strpos($word, ">") !== false) {
$insideHtmlTag = false;
$newHtmlString .= $word.' ';
continue;
}
//Word contains < character, but not >
if(strpos($word, "<") !== false) {
$insideHtmlTag = true;
$newHtmlString .= $word.' ';
continue;
}
//Word does not contain < or >
if($insideHtmlTag == false) {
$newHtmlString .= "<span>$word "."</span>";
} else {
$newHtmlString .= $word.' ';
}
} else
$newHtmlString .= $word.' ';
}
return $newHtmlString;