01c08-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 43 行
PHP
43 行
<?php// A function to check a string for bad spelling, and offer replacements.function spellcheck_string($str) { // First we must initialize pspell and tell it to use English $dict = pspell_new('en'); // Now grab every word in this string, referenced by its position $words = str_word_count($str, 2); // Reverse the words, so that we can backwards, from end to start. $rwords = array_reverse($words, true); foreach ($rwords as $pos => $word) { // If it is NOT spelled correctly if (!(pspell_check($dict, $word))) { // Determine a list of suggestions: $suggestions = pspell_suggest($dict, $word); // Generate a droplist with current word and all suggestions: $list = '<select name=\"position[{$pos}]\">'; $list .= "<option value=\"{$word}\">{$word}</option>"; foreach ($suggestions as $s) { $list .= "<option value=\"{$s}\">{$s}</option>"; } $list .= '</select>'; // Now, insert this into the original string in place // of the original word. $str = substr_replace($str, $list, $pos, strlen($word)); } } // Return the string surround by a form so that the form will display echo "<form>{$str}</form>";}$test_string = "My speling is not the bset. I oftan end up with rong characters in my sentenences.";// Spellcheck the text, output is similar to:// My [speling] is not the [bset]. I [oftan] end up with // [rong] characters in my [sentenences].spellcheck_string($test_string);?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?