2007年7月20日 星期五

用 PHP 擷取遠端網頁資訊

轉錄自用 PHP 擷取遠端網頁資訊
以下程式碼會介紹如何擷取遠端網頁資訊,包括 HTML tag 裡面的 Title, Description 及 Keywords:

PHP:
< ? php
//----- 定義要擷取的網頁地址
$url = "http://web-address";

//----- 讀取網頁源始碼
$fp = file_get_contents($url);
//highlight_string($fp); // 顯示html

//----- 擷取 title 資訊
preg_match("/< t i t l e >(.*)< \/ t i t l e >/s", $fp, $match);
$title = $match[1];

//----- 擷取 Description 及 Keywords
$metatag = get_meta_tags($url);
$description = $metatag["description"];
$keywords = $metatag["keywords"];

//----- 印出結果
echo "URL: $url\n";
echo "Title: $title\n";
echo "Description: $description\n";
echo "Keywords: $keywords\n";
? >

沒有留言: