<?php
?>
<html>
<head>
<style>
acronym.spell
{
text-decoration:underline;
color:red;
cursor:help;
}
</style>
</head>
<body>
<?php
$t = "<font color=blue>text herre & some more</font>";
echo "Before:$t";
$t = SpellCheck($t);
echo "<hr>After SpellCheck: $t";
$t = ClearSpell($t);
echo "<hr>After ClearSpell: $t";
?>
</body>
</html>
<?php
function SpellCheck($text)
{
$pspell_link = pspell_new("en"); $strings = explode(">", $text); $nStrings = count($strings);
for ($cStrings=0; $cStrings < $nStrings; $cStrings++)
{
$string = $strings[$cStrings]; if ($string =='')
continue;
$temp = explode('<', $string); $tag = $temp[1];
$cdata = $temp[0];
$subCdatas = explode(";", $cdata); $nSubCdatas = count($subCdatas); for ($cSubCdatas = 0; $cSubCdatas < $nSubCdatas; $cSubCdatas++)
{
$subCdata = $subCdatas[$cSubCdatas];
if ($subCdata == '')
continue;
$temp = explode('&', $subCdata); $subCdataEntity = $temp[1];
$subCdataWithNoEntities = $temp[0];
$subCdataWithNoEntities = fnSpell($pspell_link, $subCdataWithNoEntities); if (!$subCdataEntity ) $subCdata = $subCdataWithNoEntities;
else
$subCdata = $subCdataWithNoEntities. '&' . $subCdataEntity . ';' ;
$subCdatas[$cSubCdatas] = $subCdata; }
$cdata = implode("", $subCdatas); if ($tag) $string = $cdata . '<' . $tag . '>';
else
$string = $cdata;
$strings[$cStrings] = $string; }
$text = implode('', $strings); return $text;
}
function fnSpell($pspell_link, $string)
{
preg_match_all("/[A-Z\']{1,16}/i", $string, $words);
for ($i = 0; $i < count($words[0]); $i++)
{
$currentword = $words[0][$i];
if (!pspell_check($pspell_link, $currentword))
{
$wordarray = pspell_suggest($pspell_link, $currentword);
$words = implode(', ', $wordarray);
$suggest = "<acronym class='spell' title='$words'>$currentword</acronym class='spell'>";
$string = str_replace($currentword, $suggest, $string);
}
}
return $string;
}
function ClearSpell($text)
{
$strings = explode(">", $text);
$nStrings = count($strings);
for ($cStrings=0; $cStrings < $nStrings; $cStrings++)
{
$string = $strings[$cStrings];
if ($string =='')
continue;
$temp = explode('<', $string);
$tag = $temp[1];
$cdata = $temp[0];
if ( strstr($tag, 'acronym') && strstr($tag, "class='spell'") )
$string = $cdata;
else
$string = $cdata . '<' . $tag . '>';
$strings[$cStrings] = $string;
}
$text = implode('', $strings);
return $text;
}
?>