插件95:模式匹配单词

<?php // Plug-in 95: Pattern Match Word
/*
* 模式匹配单词
* 插件说明:
* 插件接受一个单词模式,返回一个两元素数组,其中第一个元素的值表示相匹配的单词个数,第二个元素保存单词本身。
* 若操作失败,则返回一个元素的数组,元素的值为FALSE。
* 它需要以下参数:
* $word 由字母和句点组成的单词(句点表示未知字母)
* $dictionary 字典文件和路径。
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$word = "S.e.--&$*&%$t.ng";
$result = PIPHP_PatternMatchWord($word, 'dictionary.txt');

if ($result[0] != FALSE)
{
   echo "Matches for <font face='Courier New'>" .
        "'$word'</font>:<br><ul>";
   foreach ($result[1] as $match)
      echo "<li>$match</li>";
}

function PIPHP_PatternMatchWord($word, $dictionary)
{
   // Plug-in 95: Pattern Match Word (Updated)
   //
   // This plug-in searches a dictionary of words for all those
   // matching a given pattern. Upon success it retuns a two
   // element array, the first of which is is the number of
   // matches and the second is an array containing the
   // matches. On failure it returns a single element array
   // with the value FALSE. It requires these arguments:
   //
   //    $word:       A word to look up
   //    $dictionary: The location of a list of words separated
   //                 by non-word or space characters such as
   //                 \n or \r\n

   $dict = @file_get_contents($dictionary);
   if (!strlen($dict)) return array(FALSE);
   $word = preg_replace('/[^a-z\.]/', '', strtolower($word));
   preg_match_all('/\b' . $word . '\b/', $dict, $matches);
   return array(count($matches[0]), $matches[0]);
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值