createURL($keyword, $page); if (!$mainhandler->parse($msnurl)) { break; } if (count($results) == 0) { $results = $mainhandler->items; } else { $results = array_merge($results, $mainhandler->items); } $page++; } // サブキーワード取得 $unitsearchurl = $subhandler->createURL($keyword); $subhandler->parse($unitsearchurl); $subkeyword = $subhandler->items; if (count($subhandler->items) != 0) { while (count($subkeyword) < (count($results) * 3)) { if (count($subkeyword) == 0) { $subkeyword = $subhandler->items; } else { $subkeyword = array_merge($subkeyword, $subhandler->items); } } } remove_files(OUTDIR); $date = getdate(); $filename = OUTDIR . $date["year"] . $date["mon"] . $date["mday"] . $date["hours"] . $date["minutes"] . $date["seconds"]; if ($style == "txt") { $filename .= ".txt"; } else { $filename .= ".csv"; } $fp = fopen($filename, "w"); if (!$fp) { return; } foreach($results as $item) { if ($style == "txt") { fwrite($fp, "

\n"); fwrite($fp, $item["title"] . "
\n"); fwrite($fp, $item["link"] . "
\n"); fwrite($fp, $item["description"] . "
\n"); fwrite($fp, "上記サイトはこのようなキーワードでも検索されています。"); if (count($subkeyword) > 0) { for($i = 0; $i < 3; $i++) { fwrite($fp, " \"" . array_shift($subkeyword) . "\""); } } else { fwrite($fp, " \"". $keyword . " \""); } fwrite($fp, "
\n"); fwrite($fp, "

\n"); } elseif ($style == "csv") { fwrite($fp, csv_string($item["title"]) . ","); fwrite($fp, csv_string($item["link"]) . ","); fwrite($fp, csv_string($item["description"]) . ","); fwrite($fp, csv_string("上記サイトはこのようなキーワードでも検索されています。")); if (count($subkeyword) > 0) { for($i = 0; $i < 3; $i++) { fwrite($fp, csv_string(" \"" . array_shift($subkeyword) . "\"")); } } else { fwrite($fp, csv_string(" \"". $keyword . " \"")); } fwrite($fp, "\n"); } } fclose($fp); $buff = read_tpl(TMPDIR ."result.tpl"); print(str_replace("{result_file}", $filename, $buff)); } function csv_string($str) { if (strpos($str, ",")) { $str = "\"" . $str . "\""; } return mb_convert_encoding($str, "SJIS", "auto"); } ?>