<html>
<head>


<style>
body {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  text-decoration : bold;
  background : #f3f3f3;
}

a:hover {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  background : #989898;
  text-decoration : bold;
}

a:visited, a:link, a:active {
  font-weight : normal;
  font-size : 12px;
  font-family : helvetica;
  color : #000022;
  text-decoration : normal;
}  
</style>


</head>

<body>

<b>Yahoo!</b>
<br>Keyword: <?php echo $_GET["keyword"]?>
<br>Showing <b>10</b> results 
<br><hr>
<?php

function getBaseURL($url){
   list($part1, $part2) = split("://", $url);
   list($part3, $part4) = split("/", $part2);   
#   $baseurl =  $part1 . "://" . $part3;  
   $baseurl =  $part3;  
   return $baseurl;
}


function getYahooSERP($mykeyword){

    $reg_ex = "[[:space:]]";
    $replace_word = "+"; 
    $str = $mykeyword;
    $mykeyword = ereg_replace($reg_ex, $replace_word, $str);

    $url = "http://search.yahoo.com/search;_ylt=?p=$mykeyword&n=100&";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, "http://search.yahoo.com/");
    $client = $_SERVER['HTTP_USER_AGENT'];
    curl_setopt($ch, CURLOPT_USERAGENT, "$client");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//$rowLimit = $_GET["resultLimit"];
$rowLimit = 10;

$keyword = $_GET["keyword"];
$serp = getYahooSERP($keyword);

$dom = new DOMDocument();
@$dom->loadHTML($serp);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
$sofar = "";

$intCnt = 0;
for ($i = 0; $i < $hrefs->length; $i++) {
   $href = $hrefs->item($i);
   $url = $href->getAttribute('href');
   $tmpurl = ""; 
   list($tmp1, $tmpurl) = split('\*\*', $url, 2);
   $tmpurl = urldecode($tmpurl);
   $baseurl = getBaseURL($tmpurl);

   $urlChunks = spliti (" ", $keyword);

   foreach ($urlChunks as $chunk) {
      $highChunk = '<B>'.$chunk.'</B>';
      $baseurl = str_replace("$chunk", "$highChunk", $baseurl);
   }


   $anchor = $href->getAttribute('title');
   $anchortext = $href->nodeValue; 
   if ( preg_match("/\*\*/i", "$url") )  { 

      if ( preg_match("/yahoo.com/i", "$baseurl") || preg_match("/cache/i", "$url") )  { 
      } else {
         if (preg_match("/^http/i", "$url") || preg_match("/^ftp/i", "$url")) {
            if (strpos($sofar, $baseurl) !== false) {
            } else {
               if($intCnt < $rowLimit) { 
?>
<a target=detailsframe href='kw.php?url=<?php echo $tmpurl?>&keyword=<?php echo $_GET['keyword']?>' title='<?php echo $anchortext ?>'><?php echo $baseurl ?></a><br>
<?php

                  $intCnt++; 
               } 
            }
         }
      }
   }
   $sofar = $sofar . $baseurl;
} 

?>

</body>
</html>