關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 用 PHP 擷取遠端網頁資訊

    Posted on May 8th, 2007 Sam Tang 3 comments

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

    PHP:
    1. <?php
    2. //----- 定義要擷取的網頁地址
    3. $url = "http://web-address";
    4.  
    5. //----- 讀取網頁源始碼
    6. $fp = file_get_contents($url);
    7.  
    8. //----- 擷取 title 資訊
    9. preg_match("/<title>(.*)<\/title>/s", $fp, $match);
    10. $title = $match[1];
    11.  
    12. //----- 擷取 Description 及 Keywords
    13. $metatag = get_meta_tags($url);
    14. $description = $metatag["description"];
    15. $keywords = $metatag["keywords"];
    16.  
    17. //----- 印出結果
    18. echo "URL: $url\n";
    19. echo "Title: $title\n";
    20. echo "Description: $description\n";
    21. echo "Keywords: $keywords\n";
    22. ?>


     

    3 responses to “用 PHP 擷取遠端網頁資訊”

    1. 可以問你一個語法嗎?

      cheak(); 是一個有一個,用很長時間的指令,比如file();
      語法B;要等 cheak();完成後才可以運行,
      是不是有一個php語法
      是可以先繼續運行語法B;
      到cheak();完成後再補上的.

    2. 補上PHP

      語法A;
      語法A;
      echo cheak();
      語法B;
      語法B;

      founction cheak(){
      內容A;
      內容A;
      }

    3. cheak() 傳回一個檢查值,判斷該檢查值後再執行「語法B」。
      我用一個虛擬碼寫:

      語法A;
      語法A;
      echo cheak();
      語法B;
      語法B;

      founction cheak(){
      內容A;
      內容A;
      }

      改成:
      founction cheak(){
      內容A;
      內容A;
      return boolean-value;
      }

      語法A;
      語法A;
      boolean-type return-value = cheak();
      if (return-value) {
      語法B;
      語法B;
      }

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word