2009-12-08 23 views

回答

6

還沒有看到任何圖書館,但看起來很簡單的事情。我記下了一個可以幫助你的快速功能。我一直簡單,你可能要使用捲曲來獲取內容,把一些錯誤處理等

不管怎麼說,這是我的兩分錢:

<?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/"); 
+0

謝謝,現在想弄清楚如何在ASP中編碼 – edude05 2009-12-14 16:50:12

1

約翰格魯伯具有a regex pattern,這可能有助於:

一個常見的編程問題: 識別任意 字符串文本,其中「任意」 我們同意,我們指的URL東西 非結構化如EM所有消息 或鳴叫。