|
 Gregory Patmore - 2008-05-20 13:24:45
if google doesnt return a suggestion, the class will generate a notice level error for trying to use an undefined offset.
This is easily fixed by ensuring there is a match before assigning the results. I rewrote the search function as follows
public function search($query){
$this->query = $query;
//MAKE ADDRESS
$resultado = file_get_contents(
str_replace(array('#LANGUAGE#','#QUERY#'),array($this->lang,urlencode($this->query)),
$this->search_addr)
);
//APPLY REG EXP
$this->correct = (preg_match_all("/\<div id\=res>\<p\>\<font color\=\"#cc0000\" class\=p\>(.*?)\: \<\/font\>\<a href\=\"(.*?)\" class\=p\>(.*?)\<\/a\>/i",$resultado,$matches))
? $matches[3][0]
: false;
return $this->correct;
}
 C Alex - 2008-05-24 16:56:01 - In reply to message 1 from Gregory Patmore
What i did :P
I also added the text (Did you mean: correct)
-------------------------
example
-------------------------
$DYM = new DYM();
if ($DYM->search('coreqt')){
echo $DYM->text.': '.$DYM->correct;
}
-------------------------
class
-------------------------
class DYM
{
private $query;
public $lang = 'en_US',
$search_addr = 'http://www.google.com/search?hl=#LANGUAGE#&q=#QUERY#',
$correct,
$text;
public function search($query)
{
$this->query = $query;
$result = file_get_contents(str_replace(array('#LANGUAGE#','#QUERY#'), array($this->lang, urlencode($this->query)), $this->search_addr));
preg_match_all("/\<div id\=res>\<p\>\<font color\=\"#cc0000\" class\=p\>(.*?)\: \<\/font\>\<a href\=\"(.*?)\" class\=p\>(.*?)\<\/a\>/i", $result, $matches);
$this->correct = (!empty($matches[3][0]) ? $matches[3][0] : '');
$this->text = (!empty($matches[1][0]) ? $matches[1][0] : '');
return isset($this->correct);
}
}
 C Alex - 2008-05-24 17:00:35 - In reply to message 2 from C Alex
damn...
this
-------------------------
$this->correct = (!empty($matches[3][0]) ? $matches[3][0] : '');
$this->text = (!empty($matches[1][0]) ? $matches[1][0] : '');
-------------------------
should be
-------------------------
$this->correct = (!empty($matches[3][0]) ? $matches[3][0] : NULL);
$this->text = (!empty($matches[1][0]) ? $matches[1][0] : NULL);
-------------------------
becouse if it's '' it still see's it as set
or just use
-------------------------
return !empty($this->correct);
-------------------------
instead of the isset
it's the same
Cheers
 Nuno Filipe Vieira Gomes - 2008-06-04 00:06:37 - In reply to message 1 from Gregory Patmore
Hi, this is a nice script!
But, I can't return the correct word properly to my site's search box:
<?
$DYM = new DYM();
if ($DYM->search($keyword))
{
echo $DYM->text.' '.'<a href="?tmp=1&show_save=yes&keyword='.$DYM->correct.'">'.$DYM->correct.'</a>';
}
?>
for example, for the misspelled word potugal it writes on the search box: <b><i>portugal</i></b> and not just portugal as it may be expected
Any ideas?
The best regards!
 Nuno Filipe Vieira Gomes - 2008-06-04 00:49:35 - In reply to message 4 from Nuno Filipe Vieira Gomes
Ah!
It's simple!
just edit like this:
preg_match_all("/\<div id\=res>\<p\>\<font color\=\"#cc0000\" class\=p\>(.*?)\: \<\/font\>\<a href\=\"(.*?)\" class\=p\>\<b\>\<i\>(.*?)\<\/i>\<\/b>\<\/a\>/i", $result, $matches);
Best regards ;)
 C Alex - 2008-06-04 08:24:07 - In reply to message 4 from Nuno Filipe Vieira Gomes
strip_tags() works well too...
 David M Gilbert - 2008-06-27 04:09:03 - In reply to message 6 from C Alex
here was a bug i found using this class where google was refusing access to their pages using file_get_contents from my server (localhost worked fine.).
To Fix this I added a new function aptly named file_get_contents and tricked google into thinking I was a web browser.. Full code below. I also renamed the search function to suggest to avoid incompatibilities within my search engine.
Enjoy!
class DYM{
var $query,
$correct,
$lang = 'en-US',
$m_url = 'search?hl=#LANGUAGE#&q=#QUERY#',
$m_host = "www.google.com";
function 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;
}
function suggest($query){
$this->query = $query;
$result = $this->file_get_contents(str_replace(array('#LANGUAGE#','#QUERY#'),array($this->lang,urlencode($this->query)),$this->m_url));
if (preg_match_all("/\<div id\=res>\<p\>\<font color\=\"#cc0000\" class\=p\>(.*?)\: \<\/font\>\<a href\=\"(.*?)\" class\=p\>(.*?)\<\/a\>/i",$result,$matches)) {
$this->correct = $matches[3][0];
return isSet($matches[3][0]);
}
}
}
-Dave
|