<?php
/*
USE EXAMPLE
$DYM = new DYM();
if ($DYM->suggest('Beauity Salon Durbanville')) {
echo($DYM->correct);
} else {
echo ' No suggestion ';
}
*/
class DYM {
// search string
private $query;
// The suggestion if returned
public $correct = "";
// The LANGUAGE to use in search
public $lang = 'en',
//SEARCH URL, PLEASE, FOR UPDATES, DON'T REPLACE '#LANGUAGE#' and '#QUERY#'
$m_url = 'search?hl=#LANGUAGE#&q=#QUERY#&meta=',
// The google host to use for search - try to use local
$m_host = "www.google.com";
// If you can't use file_get_contents directly due to google's security
// this fools google by imitating a browser doing search
public $use_socketconnection = false;
// method used a socket connection to get context of a remote url
// fooling the site by imitating a browser
function socket_file_get_contents($url)
{
$out = "GET /$url HTTP/1.1\r\n";
$out .= "Host: {$this->m_host}\r\n";
$out .= "User-Agent: Mozilla 4.0\r\n";
$out .= "Connection: close\r\n\r\n";
$h = fsockopen($this->m_host,80);
fwrite($h,$out);
for($a = 0,$r = '';!$a;)
{
$b = fread($h,8192);
$r .= $b;
$a = (($b=='') ? 1 : 0);
}
fclose($h);
return $r;
}
// Method used to find seggestion
function suggest($query) {
// Set the local VAR
$this->query = $query;
// Check if socket connection should be used
if ($this->use_socketconnection)
{
// get contents using socket connection
$result = $this->socket_file_get_contents(str_replace(array('#LANGUAGE#','#QUERY#'),array($this->lang,urlencode($this->query)),$this->m_url));
} else {
// get contents directly
$result = file_get_contents('http://'.$this->m_host.'/'.str_replace(array('#LANGUAGE#','#QUERY#'),array($this->lang,urlencode($this->query)),$this->m_url));
}
// Check & Apply reg-exp
if (preg_match_all("/class\=spell\>(.*?)\<\/a\>/i",$result,$matches))
{
// Set VAR and strip HTML tags
$this->correct = strip_tags($matches[1][0]);
return isSet($matches[1][0]);
}
else
{
return false;
}
}
}
?>