Get all links by reading the source files for a site

Source: Internet
Author: User
Read the source file of a site, and then use regular analysis of its source code, to get all the links
  1. /**********qiushuiwuhen (2002-5-20) ***********/
  2. if (empty ($url)) $url = "http://www.csdn.net/expert/";//Set URL
  3. $site =substr ($url, 0,strpos ($url, "/", 8));//Site
  4. $base =substr ($url, 0,strrpos ($url, "/") 1);//directory where the file is located
  5. $fp = fopen ($url, "R");//Open URL
  6. while (!feof ($FP)) $contents. =fread ($FP, 1024);//
  7. $pattern = "|href=["]? ([^ '\"] ) ['\" ]| U ";
  8. Preg_match_all ($pattern, $contents, $REGARR, preg_set_order);//Match All href=
  9. for ($i =0; $i
  10. if (!eregi ("://", $REGARR [$i][1])//is the relative path, that is, whether there is://
  11. if (substr ($regArr [$i][1],0,1) = = "/")//is the root directory of the site
  12. echo "link". ($i 1). ":". $site. $regArr [$i][1]. "
    ";//root directory
  13. Else
  14. echo "link". ($i 1). ":". $base. $regArr [$i][1]. "
    ";//Current directory
  15. Else
  16. echo "link". ($i 1). ":" $regArr [$i][1]. "
    ";//relative path
  17. }
  18. Fclose ($FP);
  19. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.