PHP method for reading the function of a website's link _ PHP Tutorial

Source: Internet
Author: User
PHP method for reading the link function of a website. Principle: read the source files of a website and use regular expressions to analyze the source code to obtain all links? * ********* Qiushuiwuhen (2002-5-20) ************ if (empty (url) urlwww. csdn. principle: read the source files of a website and analyze the source code using regular expressions to obtain all links.
   /********** Qiushuiwuhen (2002-5-20 )***********/
If (empty ($ url) $ url = "http://www.csdn.net/expert/"; // Set url
$ Site = substr ($ url, 0, strpos ($ url, "/", 8); // site
$ Base = substr ($ url, 0, strrpos ($ url, "/") + 1); // Directory of the file
  
$ Fp = fopen ($ url, "r"); // open the url
While (! Feof ($ fp) $ contents. = fread ($ fp, 1024 );//
  
$ Pattern = "| href = ["]? ([^ "] +) ["] | U ";
Preg_match_all ($ pattern, $ contents, $ regArr, PREG_SET_ORDER); // match all href =
For ($ I = 0; $ I If (! Eregi (": //", $ regArr [$ I] [1]) // whether it is a relative path, that is, whether there are ://
If (substr ($ regArr [$ I] [1],) = "/") // whether it is the root directory of the site
Echo "link". ($ I + 1). ":". $ site. $ regArr [$ I] [1]."
"; // Root directory
Else
Echo "link". ($ I + 1). ":". $ base. $ regArr [$ I] [1]."
"; // Current Directory
Else
Echo "link". ($ I + 1). ":". $ regArr [$ I] [1]."
"; // Relative path
}
Fclose ($ fp );
?>

Why? /********** Qiushuiwuhen (2002-5-20) ***********/if (empty (url )) url = "http://www.csdn ....

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.