Summary of PHP code for retrieving all links to a web page

Source: Internet
Author: User

Our core code is regular expression.

The code is as follows: Copy code

Function get_all_url ($ code ){
Preg_match_all ('/<as + href = ["|']? ([^> "'] +) [" |']? S * [^>] *> ([^>] +) </a>/I ', $ code, $ arr );
Return array ('name' => $ arr [2], 'URL' => $ arr [1]);
}

The above is just a simple example.

The code is as follows: Copy code

$ Url = 'http: // www.111cn.net ';
$ Body = @ file_get_contents ($ url );
Preg_match_all ('/href = [' "]? ([^ '"] *) ['"]?> (. *)/I ', $ body, $ B );
$ Nums = array ();
Foreach ($ B [1] as $ u ){
If (in_array ($ u, $ nums )){
Continue;
  }
$ Nums [] = $ u;
$ Title = strip_tags ($ u );
Echo $ title. "</br> ";
}

Use the curl function to obtain

The code is as follows: Copy code

<? Php
/*
* Use curl to collect all links under www.111cn.net.
*/
Include_once ('function. Php ');

$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.111cn.net /');
// Only the HTTP header is returned
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
// We do not need the page content
// Curl_setopt ($ ch, CURLOPT_NOBODY, 1 );
// Return the result instead of outputting it
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ Html = curl_exec ($ ch );
$ Info = curl_getinfo ($ ch );
If ($ html = false ){
Echo "cURL Error:". curl_error ($ ch );
}

Curl_close ($ ch );
$ Linkarr = _ striplinks ($ html );
// Host part, complete
$ Host = 'http: // www.111cn.net /';
If (is_array ($ linkarr )){
Foreach ($ linkarr as $ k => $ v ){
$ Linkresult [$ k] = _ expandlinks ($ v, $ host );
 }
}

Printf ("<p> all links on this page are: </p> <pre> % s </pre> n", var_export ($ linkresult, true ));

?>

Retrieve all URLs, but only non-duplicate data

The code is as follows: Copy code

<?
$ E = clinchgeturl ("http://im286.com/forumdisplay.php? Fid = 1 ");

Var_dump ($ e );
Function clinchgeturl ($ url)
{

// $ Url = "http: // 127.0.0.1/1.htm ";
// $ Rootpath = "http: // fsrootpathfsfsf/yyyyyy /";
// Var_dump ($ rrr );
If (eregi ('(.) * [.] (.) *', $ url )){
$ Roopath = split ("/", $ url );
$ Rootpath = "http: //". $ roopath [2]. "/";
$ Nnn = count ($ roopath)-1; for ($ yu = 3; $ yu <$ nnn; $ yu) {$ rootpath. = $ roopath [$ yu]. "/";}
// Var_dump ($ rootpath); // http:, '', 127.0.0.1, xnml, index. php
}
Else {$ rootpath = $ url; // var_dump ($ rootpath );
}
If (isset ($ url )){
Echo "$ url has the following links: <br> ";
$ Fcontents = file ($ url );
While (list (, $ line) = each ($ fcontents )){
While (eregi ('(href [[: space:] * = [[: space:] * "? [[: Alnum:]: @/. _-] [?]? [^ "] *"?) ', $ Line, $ regs )){
// $ Regs [1] = eregi_replace ('(href [[: space:] * = [[: space:] * "?) ([[: Alnum:]: @/. _-]) ("?) ', "\ 2", $ regs [1]);
$ Regs [1] = eregi_replace ('(href [[: space:] * = [[: space:] * ["]?) ([[: Alnum:]: @/. _-] [?]? [^ "] *) (. *) [^"/] * (["]?) ', "\ 2", $ regs [1]);

If (! Eregi ('^ http: //', $ regs [1]) {

If (eregi ('^ ..', $ regs [1]) {
// $ Roopath = eregi_replace ('(http ://)? ([[: Alnum:]: @/. _-]) [[: alnum:] (. *) [[[: alnum:] ', "http: // \ 2", $ url );

$ Roopath = split ("/", $ rootpath );
$ Rootpath = "http: //". $ roopath [2]. "/";
// Echo "this is the root d:". "n ";
$ Nnn = count ($ roopath)-1; for ($ yu = 3; $ yu <$ nnn; $ yu) {$ rootpath. = $ roopath [$ yu]. "/";}
// Var_dump ($ rootpath );
If (eregi ('^ .. [/[: alnum:]', $ regs [1]) {
// Echo "this is ../directory/:". "n ";
// $ Regs [1] = "../xx/xxxxxx. xx ";
// $ Rr = split ("/", $ regs [1]);
// For ($ oooi = 1; $ oooi <count ($ rr); $ oooi)
$ Rrr = $ regs [1];
// {$ Rrr. = "/". $ rr [$ oooi];
$ Rrr = eregi_replace ("^ [.] [.] [/]", '', $ rrr );/

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.