pspell_check
(PHP 4 >= 4.0.2, PHP 5)
pspell_check — Check a word
Описание
bool pspell_check
( int $dictionary_link
, string $word
)
pspell_check() checks the spelling of a word.
Список параметров
- dictionary_link
-
- word
-
The tested word.
Возвращаемые значения
Returns TRUE if the spelling is correct, FALSE if not.
Примеры
Пример #1 pspell_check() Example
<?php
$pspell_link = pspell_new("en");
if (pspell_check($pspell_link, "testt")) {
echo "This is a valid spelling";
} else {
echo "Sorry, wrong spelling";
}
?>
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Поддержка языков и кодировок
- Pspell
- pspell_add_to_personal
- pspell_add_to_session
- pspell_check
- pspell_clear_session
- pspell_config_create
- pspell_config_data_dir
- pspell_config_dict_dir
- pspell_config_ignore
- pspell_config_mode
- pspell_config_personal
- pspell_config_repl
- pspell_config_runtogether
- pspell_config_save_repl
- pspell_new_config
- pspell_new_personal
- pspell_new
- pspell_save_wordlist
- pspell_store_replacement
- pspell_suggest
Коментарии
I felt that it would help to expand on batch spell checking using this function. The previously posted example implodes using spaces as the separator for each word. There are however situations in which doing this will not return the desired result. For example, "Hello, I like coding." will return an array with two problems, "Hello," and "coding.", both these words are spelt correctly, but pspell_check() will deem them as spelled incorrectly because a comma or a period is being passed in to the function along with the word. The following example allows you to extract only the words (using regular expressions to ignore grammar such as periods or commas) in to an array and then add in html font tags to highlight all words spelled incorrectly red before returning the string.
<?
Function SpellCheck($string) {
$pspell_link = pspell_new("en");
preg_match_all("/[A-Z\']{1,16}/i", $string, $words);
for ($i = 0; $i < count($words[0]); $i++) {
if (!pspell_check($pspell_link, $words[0][$i])) {
$string = str_replace($words[0][$i], "<font color=\"#FF0000\">" . $words[0][$i] . "</font>", $string);
}
}
return $string;
}
?>
<?php
//should be using explode instead of implode
//$word = implode(" ", $message);
$word = explode(" ", $message);
foreach($word as $k => $v) {
if (pspell_check($pspell_link, $v)) {
echo "spelled right";
} else {
echo "Sorry, wrong spelling";
};
};
?>
A better pattern for splitting the words of a query up is:
preg_match_all('/[^\w\']/+/', $query, $word)
// $words has the words.