Skip to content

Instantly share code, notes, and snippets.

@deekayen
Created July 3, 2014 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deekayen/d5d4db25a4b44ac57e36 to your computer and use it in GitHub Desktop.
Save deekayen/d5d4db25a4b44ac57e36 to your computer and use it in GitHub Desktop.
This file used to work with PHP and the Pspell extension but later around PHP 4.3.1 and the release of Aspell 0.50.2, it stopped working, complaining about not finding the right dictionary.
<?php
// unset it just in case someone tried to set it from the URL with register_globals on
unset($suggestion_found);
session_start();
define("NL", "\n");
function check_textarea($message) {
return htmlspecialchars(stripslashes($message));
}
function get_suggestion($word) {
global $suggestion_found;
$suggestions = pspell_suggest(PSPELL_LINK, $word);
if(is_array($suggestions) && sizeof($suggestions) > 0) {
$suggestion_found = true;
return $suggestions;
} else
return '';
}
function merge_corrections($word_array, $corrections='') {
if(is_array($corrections)) {
foreach($corrections as $dkn => $tired) {
$word_array[$dkn] = $tired;
}
}
$input_string = (is_array($word_array)) ? str_replace(" #%newline%# ", "\r\n", implode(" ", $word_array)) : '';
session_unregister("word_array");
unset($word_array);
return $input_string;
}
function top() {
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'.NL
.' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.NL
.'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">'.NL
.'<head>'.NL
.'<title>spell checker</title>'.NL
.'</head>'.NL
.'<body>'.NL;
}
function textbox($input_string='') {
global $op;
$grn_font = '<font color="#006600">';
$end_font = '</font>';
echo '<form method="post" action="spelling.php">'.NL
.'<p><b>';
switch($op) {
case 'none':
echo $grn_font .'No spelling errors where found in this text.'. $end_font;
break;
case 'Submit Spelling':
echo $grn_font .'Finished spell checking.'. $end_font;
break;
default:
echo 'Enter the text would like to spell check:';
break;
}
echo ' </b></font></p>'.NL
.'<textarea name="input_string" wrap="virtual" cols="45" rows="12">'. check_textarea($input_string) .'</textarea><br/>'.NL
.'<input type="submit" name="op" value="Check"/>'.NL
.'</form>'.NL;
}
function make_word_array($input_string) {
//XXX - might have to use the slower preg_replace for browser compatability
// keep it here for a quick fix later in case it's needed
$input_string = str_replace("\r\n", ' #%newline%# ', stripslashes($input_string));
// $input_string = preg_replace("/\r\n|\n\r|\n|\r/", " #%newline%# ", stripslashes($input_string));
return explode(' ', $input_string);
}
function check_spelling($word_array) {
global $suggestion_found;
$suggestion_array = array();
define( PSPELL_LINK, pspell_new('en_US', '', '', '', (PSPELL_FAST|PSPELL_RUN_TOGETHER)));
for($i=0; $i<sizeof($word_array); $i++) {
// doesn't check properly if the sentence has punctuation in it
if(!empty($word_array[$i]) && ($word_array[$i] != '#%newline%#')) {
$checked = preg_replace('/\W/', '', $word_array[$i]);
// echo $checked;
// check spelling here and get suggestions
$suggestion_array[$i] = (!pspell_check(PSPELL_LINK, $checked)) ? get_suggestion($checked) : '';
} else {
$suggestion_array[$i] = '';
}
}
if($suggestion_found) {
echo '<p><font color="#990033"><b>There are misspelled words in the text body:</b></font></p>';
echo '<form method="post" action="spelling.php">';
for($i=0; $i<sizeof($suggestion_array); $i++) {
$suggestions = $suggestion_array[$i];
if(is_array($suggestions)) {
echo '<select name="corrections['. $i .']">'.NL;
echo ' <option value="', htmlspecialchars($word_array[$i]) .'" selected>'. htmlspecialchars($word_array[$i]) .'</option>'.NL;
$punctuation = array(',','.','!','?','*','>','<','{','}','[',']',':',';','"','&','%','@','#','^','~');
foreach($suggestions as $option) {
if(in_array(substr($word_array[$i], 0, 1), $punctuation))
$option = substr($word_array[$i], 0, 1) . $option;
if(in_array(substr($word_array[$i], -1), $punctuation))
$option .= substr($word_array[$i], -1);
echo ' <option value="'. $option .'">'. htmlspecialchars($option) .'</option>'.NL;
}
echo '</select>'.NL;
} else {
echo ($word_array[$i]=="#%newline%#") ? '<br/>' : $word_array[$i].' ';
}
}
echo '<br/><input type="submit" name="op" value="Submit Spelling"/>';
echo '</form>'.NL;
} else {
header("location: ". getenv("PHP_SELF") ."?op=none");
}
}
function bottom() {
echo '</body>'.NL
.'</html>';
}
switch($op) {
case "Submit Spelling":
$input_string = merge_corrections($word_array, $corrections);
top();
textbox($input_string);
bottom();
break;
case "Check":
session_register("word_array");
$word_array = make_word_array($input_string);
check_spelling($word_array);
break;
default:
top();
$input_string = merge_corrections($word_array);
textbox($input_string);
bottom();
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment