Login   Register  
PHP Classes
elePHPant
Icontem

bug

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Google - Did You Mean  >  All threads  >  bug  >  (Un) Subscribe thread alerts  
Subject:bug
Summary:no match generates notice: line 34 undefined offset: 0
Messages:7
Author:Gregory Patmore
Date:2008-05-20 13:24:43
Update:2008-06-27 04:09:03
 

  1. bug   Reply   Report abuse  
Picture of Gregory Patmore
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;
}

  2. Re: bug   Reply   Report abuse  
Picture of Calinescu Alex
Calinescu 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);
}
}

  3. Re: bug   Reply   Report abuse  
Picture of Calinescu Alex
Calinescu Alex
2008-05-24 17:00:35 - In reply to message 2 from Calinescu 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

  4. Re: bug   Reply   Report abuse  
Picture of Nuno Filipe Vieira Gomes
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!

  5. Re: bug   Reply   Report abuse  
Picture of Nuno Filipe Vieira Gomes
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 ;)

  6. Re: bug   Reply   Report abuse  
Picture of Calinescu Alex
Calinescu Alex
2008-06-04 08:24:07 - In reply to message 4 from Nuno Filipe Vieira Gomes
strip_tags() works well too...

  7. Re: bug   Reply   Report abuse  
Picture of David M Gilbert
David M Gilbert
2008-06-27 04:09:03 - In reply to message 6 from Calinescu 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