Php collection getting started tutorial, teach you how to write collection-PHP source code

Source: Internet
Author: User
Tags preg
Ec (2); php collection getting started tutorial, teach you how to write collection. Our first step is to collect all the connections. This is not a simple collection article, what we need to do is to collect the entire book and save it to a text, because now MP3 is popular and you can read e-books. How can I save a book? Of course, we need to store the book name for search and pull. Let's first collect the title of this book. Let's take a look at the prototype: & lt; metaname & quot; description & quot; content & q script ec (2); script

Php collection getting started tutorial, teach you how to write collection

The first step is to collect all the connections. We are not simply collecting an article. What we need to do is to collect the entire book and save it to a text, because MP3 is now popular, all of them can read e-books.
How can we store a book? Of course, we need to store the book name for search and pull. Let's collect the title of this book first,
Let's take a look at the prototype:

The rule is:

Let's write a regular expression. Don't tell me that I won't. I won't come to Hunan to pull it.
Regular Expression:

Start construction now! First, we need to obtain resources. here we need to use a function:
File_get_contents ()
Introduction:
Main function: Read the entire file into a string
Original form: string file_get_contents
(String filename [, bool use_include_path [, resource context [, int offset [, int maxlen])


What does it mean? It actually tells you to search for a conforming string in a resource and assign it to a variable.
The above is the first thing we need to use. If we know one thing, we will start to write something so that we can better understand it and remember it. I will analyze the idea of writing a program:
If we collect an address, we will not just collect a book. So our collection address is changed. What is the purpose of the change? At this time, I threw a huge piece of chalk. Didn't I tell you? Variable, a stern teacher Wang Jianjun, used up all his strength and threw it over the chalk. I wanted to cry ....... The teacher beat me !!!!!!!! Let's take a look.
If the variable is used, use the variable. We get the address. The Code is as follows:
$ Url = "http://book.sina.com.cn/nzt/lit/zhuxian2/index.shtml"; // book address
With the above description, we should be able to write it completely now and start the Code:


//************************************** **************************


$ Url = "http://book.sina.com.cn/nzt/lit/zhuxian2/index.shtml"; // book address


$ Ver = "old"; // new or old version


// Because there are two types of pages for a book, we need to differentiate them here

//************************************** **************************


// Obtain the Page code file_get_contents () to read the file into a string, which must be used below


$ R = file_get_contents ($ url );


// Search for the title in the string obtained above, and assign the value to the variable $ booktitle. $ booktitle is an array./is can be understood as the beginning!


Preg_match ("/ /Is ", $ r, $ booktitle );


// Assign the first captured title to the variable bookname.


$ Bookname = $ booktitle [1]; // Title


// Print_r ($ booktitle); die (); output if you don't understand it. Hey, it helps you understand it.


/*************************************** **************************************** ******


* Prototype:

  • Chapter 2 pain points (1)


    * The rule is:

  • Not fixed


    * ISU is a regular mode. This mode is not greedy, that is, it ends when matching.


    **************************************** **************************************** *****/


    $ Preg = '/

  • /IsU ';


    /*************************************** **************************************** *


    * Preg_match_all: Global Regular Expression matching


    * Prototype:


    *
    Int preg_match_all


    *


    (String pattern, string subject, array matches [, int flags])


    * Meaning: In the global search resource variable $ preg, get an array and assign a value to the variable $ zj, which is the array.


    * You can use the marker to retrieve the resources in the resource list. Instead, you can view the array!


    * Teacher Wang said that he would not give me an array of books. When will he come in?


    **************************************** **************************************** **/


    Preg_match_all ($ preg, $ r, $ zj );


    // Print_r ($ zj); die (); output if you don't understand it. Hey, it helps you understand it.


    // Calculate the number of titles. I asked you how many chapters are displayed and how many chapters are collected.


    $ Bookzj = count ($ zj [1]);


    // Determine the plate type you want to collect. Because the content is different, it can be automatically determined. I also wrote it, but it is not published, because it is very simple.


    If ($ ver = "new "){

    $ Content_start =" ";

    $ Content_end =" ";

    }


    If ($ ver = "old "){

    $ Content_start =" ";

    $ Content_end ="
    ";

    }


    // Process the collected file and set the encoding. Why is this? Because you can check the website source code !!!

    Header ("Content-Type: text/html; charset = gb2312 ");

    /*************************************** **************************************** **********

    * One merge of content from 1 to 136 pages. This is the best one... copyright protection to avoid infringement. Hey hey, it seems that I am infringing it !!!

    * XX must want to murder. This sentence means to write a copyright and create a file.

    **************************************** **************************************** *********/

    Writer ($ bookname. "Total ". $ bookzj. "Section rn handsome Liu binyu ". date ("d m j G: I: s T Y "). "Design novels for graduation to collect rn ",". /ljy /". $ bookname. ". txt "," w + ");

    /*************************************** **************************************** **********

    * One merge of content from 1 to 136 pages. This is the best one... copyright protection to avoid infringement. Hey hey, it seems that I am infringing it !!!

    * XX must want to murder. This sentence means to write a copyright and create a file.

    **************************************** **************************************** *********/

    For ($ I = 0; $ I <$ bookzj; $ I ++) {// The following prompt is displayed: What is the frontend in $ bookzj and you are calling for output?


    // Echo "http://book.sina.com.cn". $ zj [1] [$ I] ". shtml"; die ();


    $ Str = file_get_contents ("http://book.sina.com.cn". $ zj [1] [$ I]. ". shtml ");


    Preg_match ("/()(.*?) ()/Is ", $ str, $ title );


    $ Title = str_replace ("_ _ Sina", "", preg_replace ("/<(. *?)> /S "," ", $ title [2]);


    /*************************************** ************************************


    * Preg_replace: search and replace regular expressions


    * The usage of str_replace is really hard to say. Let's look at the example! It is actually a replacement.


    * Str = "abcabc". replace (/a/g, "d"); // The result is dbcdbc.


    * Str = "abcabc". replace (/a/, "d"); // The result is dbcabc.


    **************************************** ***********************************/


    Preg_match ("/(". $ content_start .")(.*?) (". $ Content_end.")/is ", $ str, $ content );


    $ Content = preg_replace ("/<(. *?)> /S "," ", str_replace ("

    "," Rn ", $ content [2]);


    $ Content = str_replace ("
    "," ", Preg_replace ("/^ [s] * n/is "," ", $ content ));


    $ Content = str_replace ("? "," ", Preg_replace ("/^ [s] * n/is "," ", $ content ));

    $ Result = "rn no.". ($ I + 1). "section --------". $ title. "_ Miss Wang is Shuai --------- rn". $ content;


    // Var_dump ($ result); die ();


    Writer ($ result, "./ailaopo/". $ bookname. ". txt", "a + ");


    Echo "Novels". $ bookname. "Total". $ bookzj. "section, now go to section". $ I. "_". $ title ."
    ";

    }
    Echo "Novels". $ bookname. "Total". $ bookzj. "The section has been fully organized! ";


    Function writer ($ content, $ url, $ mode)
    {
    $ Fp = fopen ($ url, $ mode );
    Fwrite ($ fp, $ content );
    Fclose ($ fp );
    }
    ?>

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