c as $correction) { $suggestions = explode("\t", (string)$correction); $offset = $correction['o']; $length = $correction['l']; $replacement[mb_substr($result, $offset, $length)] = $suggestions[0]; } foreach($replacement as $old => $new) { $old = preg_quote($old); $result = preg_replace("/$old/is", $new, $result, 1); } return $result; } /** * Get the XML response object containing spelling suggestions for a query. * * @param string query The query to check for spelling mistakes * @param string lang Language * @param string hl Human interface language * * @param string Google's XML response containing suggested alternative spellings for a query. */ public static function GetSuggestions( $query, $lang='en', $hl='en' ) { if( !is_null(self::$_cache[$query]) ) { return self::$_cache[$query]; } $post = ''.htmlspecialchars($query).''; $server = "www.google.com"; $path = "/tbproxy/spell?lang=$lang&hl=$hl"; $url = "https://www.google.com"; $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($post)." \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"; $header .= $post; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header); $data = curl_exec($ch); if (curl_errno($ch)) { throw new Exception( curl_error($ch) ); } else { curl_close($ch); } $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($xml_parser, $data, $vals, $index); xml_parser_free($xml_parser); self::$_cache[$query] = $data; // Returns the same data you'd get form Google toolbar return $data; } }