是否有一个模拟Facebook“链接检测”的库?

我正在寻找一个库,当你发布链接时“解析”像facebook这样的信息。 然而,因为我不想重新发明轮子有没有人知道图书馆或努力编写一个已经这样做的图书馆?

我已经包含了一个例子,这样你就可以掌握我的意思,如果你不使用脸书。 http://lh4.ggpht.com/_zbED-KN_ZAI/Sx6LuDmZkVI/AAAAAAAADLs/mN7eFnzL1gE/s144/example.png

没有看过任何图书馆,但看起来很简单。 我已经记下了一个可以帮助你的快速function。 我保持简单,您可能希望使用cURL来获取内容,进行一些error handling等。

无论如何,这是我的两分钱:

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 = << 
{$linkTitle}
{$linkTitle}
{$url}
{$linkDesc}
HTML; return $output; } echo getLinkInfo("http://www.phpfour.com/");

John Gruber有一个可能有帮助的正则表达式模式 :

一个常见的编程问题:在任意文本字符串中识别URL,其中“任意”让我们同意我们指的是非结构化的东西,例如电子邮件或推文。