插件95:模式匹配单词

来源:互联网 发布:服装店创业知乎 编辑:程序博客网 时间:2024/05/16 11:23
<?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]);}?>

原创粉丝点击