我正在尋找一個圖書館,「解析」信息就像Facebook發佈鏈接時所做的那樣。然而,因爲我不想重新發明輪子,是否有人知道一個圖書館或努力編寫一個圖書館已經這樣做?有沒有模仿Facebook的「鏈接檢測」的庫?
我已經包含了一個例子,以便您可以掌握我的意思,如果你不使用臉書。 http://lh4.ggpht.com/_zbED-KN_ZAI/Sx6LuDmZkVI/AAAAAAAADLs/mN7eFnzL1gE/s144/example.png
我正在尋找一個圖書館,「解析」信息就像Facebook發佈鏈接時所做的那樣。然而,因爲我不想重新發明輪子,是否有人知道一個圖書館或努力編寫一個圖書館已經這樣做?有沒有模仿Facebook的「鏈接檢測」的庫?
我已經包含了一個例子,以便您可以掌握我的意思,如果你不使用臉書。 http://lh4.ggpht.com/_zbED-KN_ZAI/Sx6LuDmZkVI/AAAAAAAADLs/mN7eFnzL1gE/s144/example.png
還沒有看到任何圖書館,但看起來很簡單的事情。我記下了一個可以幫助你的快速功能。我一直簡單,你可能要使用捲曲來獲取內容,把一些錯誤處理等
不管怎麼說,這是我的兩分錢:
<?php
function getLinkInfo($url)
{
// Get target link html
$html = file_get_contents($url);
// Prepare the DOM document
$dom = new DOMDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
// Get page title
$titles = $dom->getElementsByTagname('title');
foreach ($titles as $title) {
$linkTitle = $title->nodeValue;
}
// Get META tags
$metas = $dom->getElementsByTagname('meta');
// We only need description
foreach ($metas as $meta) {
if ($meta->getAttribute("name") == "description") {
$linkDesc = $meta->getAttribute("content");
}
}
// Get all images
$imgs = $dom->getElementsByTagname('img');
// Again, we need the first one only
foreach ($imgs as $img) {
$firstImage = $img->getAttribute("src");
if (strpos("http://", $firstImage) === false) {
$firstImage = $url . $firstImage;
}
break;
}
$output = <<<HTML
<div class="info">
<div class="image"><img src="{$firstImage}" alt="{$linkTitle}" /></div>
<div class="desc">
<div class="title">{$linkTitle}</div>
<div class="subtitle">{$url}</div>
<div class="summary">{$linkDesc}</div>
</div>
</div>
HTML;
return $output;
}
echo getLinkInfo("http://www.phpfour.com/");
約翰格魯伯具有a regex pattern,這可能有助於:
一個常見的編程問題: 識別任意 字符串文本,其中「任意」 我們同意,我們指的URL東西 非結構化如EM所有消息 或鳴叫。
謝謝,現在想弄清楚如何在ASP中編碼 – edude05 2009-12-14 16:50:12