Using PHP function skillfully to implement _php tutorial of collector

Source: Internet
Author: User
PHP after a long period of development, many users are very familiar with PHP, we can now use PHP functions to implement the Collector program. What is the collector, usually called the Thief program, mainly used to crawl other people's web content. About the production of the collector, in fact, is not difficult, is to open the Web to be collected remotely, and then use regular expressions to match the required content, as long as a little bit of the basis of regular expression, can make their own collector.

A few days ago did a novel serial program, because afraid of updating trouble, incidentally wrote a collector, the acquisition of eight Chinese network, the function is relatively simple, can not customize the rules, but the idea is in the inside, the custom rules can be extended by themselves. Using PHP to do the collector is mainly used to two PHP functions: file_get_contents () and Preg_match_all (), the previous is to read the Web page content, but only in the version of PHP5 above to use, the latter is a regular function, to extract the required content. The face is a step-by-step function implementation. Because it is a collection of novels, so first of all, the title, author, type of the three extracted, other information can be extracted according to needs.

This is not enough and requires 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 the place to start, $end for the end. Remove 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. So, $typeid [0] is the classification number we're looking for. Here's how:
  10. $ Ustart = "" ";
  11. $ Uend
  12. = "" ";
  13. T denotes the abbreviation of title
  14. $ Tstart = ">" ;
  15. $ tend
  16. = "<" ;
  17. Take the path, for example: 123.shtm,2342.shtm,233.shtm
  18. Preg_match_all ("/" [0-9]{1,}. ( shtm) "/is", $chapterurl, $url);
  19. Title, for example: Chapter Nine The Righteous
  20. Preg_match_all ("/<a href=" [0-9]{1,}. Shtm "(. *?) < /A > /is ", $file, $title);
  21. $ Count Countcount = 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 array is all the chapter address, here, the collector is half done, the rest is to loop open each chapter address, read, and then match the content. This is relatively simple and is not described in detail here. Well, write this today first, the first time to write such a long article, language organization inevitably have problems, but also please forgive us!


http://www.bkjia.com/PHPjc/446461.html www.bkjia.com true http://www.bkjia.com/PHPjc/446461.html techarticle PHP After a long period of development, many users are very familiar with PHP, we can now use PHP functions to implement the Collector program. What is the collector, usually called the Thief program, is mainly used to catch ...

  • Related Article

    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.