2012-11-05 45 views
-2

可能重複:
How to extract img src, title and alt from html using php?提取標題

我想借此當你發佈一個新地址,它從Digg.com複製某些功能自動掃描網址並找到頁面標題。

請告訴它是如何在PHP做......有沒有其他任何可用的管理系統,通過它可以使網站如Digg

+3

歡迎來到SE - 您的問題,因爲它表明沒有研究。你試過什麼了? – Andrew

+0

您需要使用'file_get_contents()'從獲取的頁面獲取標題標記。看到這個:http://stackoverflow.com/questions/3711357/get-title-and-meta-tags-of-external-site –

回答

0

你可以做到這一點使用Ajax調用服務器,在那裏你捲曲URL併發回你想要的所有細節。您可能會感興趣的標題,描述,關鍵詞等

+1

你能告訴網站從哪裏我可以學到它 – user1800923

+0

@用戶可以使用谷歌? – Gordon

1

您可以使用file_get_contents()函數從頁面獲取數據,然後使用一個正則表達式一起的preg_match()之間來獲取數據<title></title>

'/<title>(.*?)<\/title>'/ 
+1

將失敗的標題有屬性 – Gordon

+0

是的 - 只是舉一個例子:) – Prash

0
function get_title($url) { 
    $ch = curl_init(); 
    $titleName = ''; 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 


    // data will contain the whole page you are looking for 
    // You need to parse it for the string like this <title>Google</title> 
    // start = strrpos($data, '<title>'); 
    // end = strrpos($data, '</title>'); 
    // substr($data, $start + 6, $end); 6 - length of title 
    return $titleName; 
} 

您需要實施更智能的解析方式,因爲<title > Google < /title>它不會找到。