note 78609 added to ref.pspell
| From: | dylan at wedefy dot com | Date: | Fri, 19 Oct 2007 00:36:00 +0000 |
| Subject: | note 78609 added to ref.pspell | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131523@lists.php.net to get a copy of this message | ||
preg_replace_callback() is the best way I found to check words in a large text and change the HTML
color of words that are not found. Here is a script I wrote which does just that, and optionally
uses pspell_suggest() to auto-correct. I would love to write a better regex to ignore <text
inside xml tags>, but that really isn't as easy as it sounds...
<?php
$pspell =
pspell_new('en','canadian','','utf-8',PSPELL_FAST);
function spellCheckWord($word) {
global $pspell;
$autocorrect = TRUE;
// Take the string match from preg_replace_callback's array
$word = $word[0];
// Ignore ALL CAPS
if (preg_match('/^[A-Z]*$/',$word)) return $word;
// Return dictionary words
if (pspell_check($pspell,$word))
return $word;
// Auto-correct with the first suggestion, color green
if ($autocorrect && $suggestions = pspell_suggest($pspell,$word))
return '<span
style="color:#00FF00;">'.current($suggestions).'</span>';
// No suggestions, color red
return '<span style="color:#FF0000;">'.$word.'</span>';
}
function spellCheck($string) {
return preg_replace_callback('/\b\w+\b/','spellCheckWord',$string);
}
echo spellCheck('PHP is a reflecktive proegramming langwage origenaly dezigned for prodewcing
dinamic waieb pagges.');
?>
OUTPUT: PHP is a reflective programming language originally designed for producing dynamic Web
pages.
----
Server IP: 216.235.15.211
Probable Submitter: 142.134.150.104
----
Manual Page -- http://www.php.net/manual/en/ref.pspell.php
Edit -- https://master.php.net/note/edit/78609
Del: integrated -- https://master.php.net/note/delete/78609/integrated
Del: useless -- https://master.php.net/note/delete/78609/useless
Del: bad code -- https://master.php.net/note/delete/78609/bad+code
Del: spam -- https://master.php.net/note/delete/78609/spam
Del: non-english -- https://master.php.net/note/delete/78609/non-english
Del: in docs -- https://master.php.net/note/delete/78609/in+docs
Del: other reasons-- https://master.php.net/note/delete/78609
Reject -- https://master.php.net/note/reject/78609
Search -- https://master.php.net/manage/user-notes.php