Source for file GoogleSpell.php
Documentation is available at GoogleSpell.php
* $Id: GoogleSpell.php,v 1.2 2009/08/09 03:08:08 sam2kb Exp $
* @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
* Spellchecks an array of words.
* @param {String} $lang Language code like sv or en.
* @param {Array} $words Array of words to spellcheck.
* @return {Array} Array of misspelled words.
for ($i=
0; $i<
count($matches); $i++
)
* Returns suggestions of for a specific word.
* @param {String} $lang Language code like sv or en.
* @param {String} $word Specific word to get suggestions for.
* @return {Array} Array of suggestions for the specified word.
foreach ($sug as $item) {
$server =
"www.google.com";
$path =
"/tbproxy/spell?lang=" .
$lang .
"&hl=en";
$host =
"www.google.com";
$url =
"https://" .
$server;
$xml =
'<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' .
$str .
'</text></spellrequest>';
$header =
"POST ".
$path.
" HTTP/1.0 \r\n";
$header .=
"MIME-Version: 1.0 \r\n";
$header .=
"Content-type: application/PTI26 \r\n";
$header .=
"Content-length: ".
strlen($xml).
" \r\n";
$header .=
"Content-transfer-encoding: text \r\n";
$header .=
"Request-number: 1 \r\n";
$header .=
"Document-type: Request \r\n";
$header .=
"Interface-Version: Test 1.4 \r\n";
$header .=
"Connection: close \r\n\r\n";
$fp =
fsockopen("ssl://" .
$server, $port, $errno, $errstr, 30);
echo
"Could not open SSL connection to google.";
// Grab and parse content
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
$string =
preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string =
preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
return strtr($string, $trans_tbl);
// Patch in multibyte support
function mb_substr($str, $start, $len =
'', $encoding=
"UTF-8"){
for ($s =
0; $start >
0;--
$start) {// found the real start
while ($str[$s] >=
"\x80" &&
$str[$s] <=
"\xBF")
for ($e =
$s; $len >
0; --
$len) {//found the real end
while ($str[$e] >=
"\x80" &&
$str[$e] <=
"\xBF" &&
$e <
$limit)
return substr($str, $s, $e -
$s);