Php document collection regular code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php articles collect regular code. Copy the code as follows: collect htmlfunctiongetwebcontent ($ url) {$ chcurl_init (); $ timeout10; curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER The code is as follows:


// Collect html
Function getwebcontent ($ url ){
$ Ch = curl_init ();
$ Timeout = 10;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
$ Contents = trim (curl_exec ($ ch ));
Curl_close ($ ch );
Return $ contents;
}


// Obtain the title and url
$ String =
Getwebcontent ('http: // www. ***. com/learn/zhunbeihuaiyun/jijibeiyun/2 ');
// Regular expression matching

  • Get title and address
    Preg_match_all ("/
  • (. *) <\/A>/", $ string, $ out, PREG_SET_ORDER );
    Foreach ($ out as $ key => $ value ){
    $ Article ['title'] [] = $ out [$ key] [2];
    $ Article ['link'] [] = "http: // www. ***. com/learn/article/". $ out [$ key] [1];
    }
    // Obtain the document content based on the url
    Foreach ($ article ['link'] as $ key => $ value ){
    $ Content_html = getwebcontent ($ article ['link'] [$ key]);
    Preg_match ("/

    [\ S | \ S] *? <\/P>/", $ content_html, $ matches );
    $ Article [content] [$ key] = $ matches [0];

    }
    // It cannot be saved as a file without transcoding.
    Foreach ($ article [title] as $ key => $ value ){
    $ Article [title] [$ key] = iconv ('utf-8', 'gbk', $ value); // transcoding
    }
    // Save the file
    $ Num = count ($ article ['title']);
    For ($ I = 0; $ I <$ num; $ I ++ ){
    File_put_contents ("registry.article=title={ I }.txt", $ article ['content'] [$ I]);
    }
    ?>

    The http://www.bkjia.com/PHPjc/321079.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321079.htmlTechArticle code is as follows: // collect html function getwebcontent ($ url) {$ ch = curl_init (); $ timeout = 10; curl_setopt ($ ch, CURLOPT_URL, $ url ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER...

  • 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.