forked from LPology/Javascript-PHP-Spell-Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellcheck.php
43 lines (35 loc) · 994 Bytes
/
spellcheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
/**
* Javascript/PHP Spell Checker
* Version 1.6
* https://github.com/LPology/Javascript-PHP-Spell-Checker
*
* Copyright 2012-2015 LPology, LLC
* Released under the MIT license
*
* Requires the Pspell extension
* http://www.php.net/manual/en/book.pspell.php
*/
if (isset($_REQUEST['text'])) {
$text = $_REQUEST['text'];
} else {
exit(json_encode(array('success' => false)));
}
if (!$pspell = pspell_new('en', '', '', '', PSPELL_FAST)) {
exit(json_encode(array('success' => false)));
}
$words = preg_split('/[\W]+/u', $text, -1, PREG_SPLIT_NO_EMPTY);
$misspelled = array();
$return = array();
foreach ($words as $w) {
if (!pspell_check($pspell, $w) && !is_numeric($w)) {
$misspelled[] = $w;
}
}
if (sizeof($misspelled) < 1) {
exit(json_encode(array('success' => true, 'errors' => false)));
}
foreach ($misspelled as $m) {
$return[$m] = pspell_suggest($pspell, $m);
}
echo json_encode(array('success' => true, 'errors' => true, 'words' => $return));