Clever use of PHP functions to implement collector _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Clever use of PHP functions to implement collectors. PHP has been developing for a long time and many users are familiar with PHP. now we can use PHP functions to implement the collector program. What is a collector? it is usually called a thief program. it is mainly used to catch PHP. after a long period of development, many users know PHP very well. now we can use PHP functions to implement the collector program. What is a collector, usually known as a thief program, is mainly used to capture others' webpage content. It is not difficult to create a collector, that is, to remotely open the webpage to be collected, and then use a regular expression to match the required content, as long as there is a little basis for a regular expression, you can make your own collectors.

I developed a novel serialization program a few days ago. for fear of updating problems, I wrote a collector to collect 8-way Chinese network. the function is simple and cannot be customized, however, the general idea is in place, and custom rules can be expanded by themselves. Using php as the collector mainly uses two PHP functions: file_get_contents () and preg_match_all (). The first one reads the webpage content remotely, but it can only be used in versions later than php5, the latter is a regular function used to extract the required content. In one step. Because it is a collection of novels, we must first extract the title, author, and type. Other information can be extracted as needed.

This is not enough. you also need a PHP function to be cut:

 
 
  1. Function cut ($ string, $ start, $ end ){
  2. $Message=Explode($ Start, $ string );
  3. $Message=Explode($ End, $ message [1]); return $ message [0];} where $ string is the content to be cut, $ start is the start, $ end indicates the end point. Retrieve the classification number:
  4. $Start="Html/Book /";
  5. $End
  6. ="List. shtm";
  7. $Typeid=Cut($ Typeid [0] [0], $ start, $ end );
  8. $Typeid=Explode("/", $ Typeid); [/php]
  9. In this way, $ typeid [0] is the classification number we are looking. The method is as follows:
  10. $Ustart=""";
  11. $Uend
  12. =""";
  13. // T stands for title
  14. $Tstart=">";
  15. $Tend
  16. ="<";
  17. // Obtain the path, for example, 123. shtm, 2342. shtm, 233. shtm.
  18. Preg_match_all ("/" [0-9] {1,}. (shtm) "/is", $ chapterurl, $ url );
  19. // Obtain the title, for example, Chapter 1 "9 ".
  20. Preg_match_all ("/<A Href= "[0-9] {1,}. shtm "(.*?)</>/Is ", $ file, $ title );
  21. $CountCountcount= Count ($ url [0]);
  22. For ($I=0; $ I<= $ Count; $ I ++)
  23. {
  24. $U=Cut($ Url [0] [$ I], $ ustart, $ uend );
  25. $T=Cut($ Title [0] [$ I], $ tstart, $ tend );
  26. $ Array [$ u] = $ t;
  27. }

$ Array is the address of all chapters. here, the collector completes the process in half. The rest is to open the address of each chapter in a loop, read the address, and match the content. This is relatively simple and will not be described in detail here. Now, let's write this article first today. for the first time I write such a long article, there will inevitably be problems with language organization. please forgive me!


Bytes. What is a collector, usually called a thief program, mainly used to capture...

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.