插件8:拼写检查

插件8:拼写检查<?php//Plug-in8:SpellCheck//Thisisanexecutableexamplewithadditionalcodesupplie

大家好,又见面了,我是你们的朋友全栈君。原文地址为:
插件8:拼写检查

<?php // Plug-in 8: Spell Check

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$text = "It was the best of tmes, it was the wrst of times, it was the age of wisdom, it was the age of foolishnes, it was the epoch of beleif, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despare, we had everything before us, we had nothing before us, we were all going drect to heaven.";

echo "$text<br /><br />";
echo PIPHP_SpellCheck($text, "u");

function PIPHP_SpellCheck($text, $action)
{
   // Plug-in 8: Spell Check
   //
   // This plug-in takes a string of text and then highlights
   // those words it doesn't recognise in the dictionary file.
   // The arguments required are:
   //
   //    $text:    Text to be modified
   //    $action:  What to do with unrecognized words:
   //              "u", "b", "i" = Underline, Bold or Italic

   $dictionary = PIPHP_SpellCheckLoadDictionary("dictionary.txt");
   $text      .= ' ';
   $newtext    = "";
   $offset     = 0;

   while ($offset < strlen($text))
   {
      preg_match('/[^\w]*([\w]+)[^\w]+/',
         $text, $matches, PREG_OFFSET_CAPTURE, $offset);
      $word   = $matches[1][0];
      $offset = $matches[0][1] + strlen($matches[0][0]);
      
      if (!PIPHP_SpellCheckWord($word, $dictionary))
         $newtext .= "<$action>$word</$action> ";
      else $newtext .= "$word ";
   }
   
   return rtrim($newtext);
}

function PIPHP_SpellCheckLoadDictionary($filename)
{
   return explode("\r\n", file_get_contents($filename));
}

function PIPHP_SpellCheckWord($word, $dictionary)
{
   $top = sizeof($dictionary) -1;
   $bot  = 0;
   $word = strtolower($word);

   while($top >= $bot)
   {
      $p =   floor(($top + $bot) / 2);
      if     ($dictionary[$p] < $word) $bot = $p + 1;
      elseif ($dictionary[$p] > $word) $top = $p - 1;
      else   return TRUE;
   }
     
   return FALSE;
}

?>

1. 插件说明:

插件8需要两个参数。一个代表需要拼写检查的字符串,另一个表示拼写检查后文本的显示方式。它们是:

$text 字符串变量,表示需要拼写检查的文本。

$action 字符串变量,它是一个字母,表示文本显示的格式。

dictionary.txt:

《太长了,省略。》

 

转载请注明本文地址:
插件8:拼写检查

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/131120.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号