A PHP Text Collection class

Source: Internet
Author: User
    1. $C = new Collection ();
    2. $C->url = ' Http://bbs.it-home.org/subject_search?cat=1001&search_text=%E5%B9%B4%E8%BD%BB%E4%BA%BA ';
    3. $C->startflag = '

      ';

    4. $C->endflag = ";
    5. $C->init ();
    6. $C->regexp = "| (.*)

      (.*)

      | Uis ";
    7. $C->parse ();
    8. PRINT_RR ($C->result);
    9. */
Copy Code

PHP text capture class file:

  1. <%

  2. /**
  3. Module Name: PHP Text Collection Class
  4. **/
  5. Class collection{
  6. Entrance public
  7. var $url; URL address to analyze
  8. var $content; What to read
  9. var $regExp; To get a partial regular expression
  10. var $codeFrom; Encoding of the original text
  11. var $codeTo; Encoding to convert
  12. var $timeout; Time to collect wait

  13. var $startFlag; The flag that the article starts to collect by default is 0 when collecting entries, only the text blocks between $startflag and $endFlag are searched and collected.

  14. var $endFlag; End of the article collection of the flag by default for the end of the article in the collection of entries, only the $startflag and $endFlag between the text block search and acquisition.
  15. var $block; Block of text between $startFlag and $endFlag
  16. Export Private
  17. var $result; Output results

  18. Initializing the Collector

  19. function init () {
  20. if (empty ($url))
  21. $this->getfile ();
  22. $this->convertencoding ();
  23. }
  24. Capture the required content
  25. Function Parse () {
  26. $this->getblock ();
  27. Preg_match_all ($this->regexp, $this->block, $this->result,preg_set_order);
  28. return $this->block;
  29. }
  30. Error handling
  31. Function error ($MSG) {
  32. Echo $msg;
  33. }
  34. Read Remote Web page If successful, returns the file, or False if the failure is returned
  35. function GetFile () {
  36. $datalines = @file ($this->url);
  37. if (! $datalines) {
  38. $this->error ("can ' t read the URL:". $this->url);
  39. return false;
  40. } else {
  41. $importdata = Implode (' ', $datalines);
  42. $importdata = Str_replace (Array ("\ r \ n", "\ R"), "\ n", $importdata);
  43. $this->content = $importdata;
  44. }
  45. }
  46. Get the block of text you want
  47. function Getblock () {
  48. if (!empty ($this->startflag))
  49. $this->block = substr ($this->content,strpos ($this->content, $this->startflag));
  50. if (!empty ($this->endflag))
  51. $this->block = substr ($this->block,0,strpos ($this->block, $this->endflag));
  52. }
  53. Conversion of content encoding
  54. function convertencoding () {
  55. if (!empty ($this->codeto))
  56. $this->codefrom = mb_detect_encoding ($this->content);
  57. The conversion is performed if a conversion scenario is given.
  58. if (!empty ($this->codeto))
  59. $this->content = mb_convert_encoding ($this->content, $this->codeto, $this->codefrom) or $this->error ( "Can ' t convert Encoding");
  60. }
  61. }//end of Class
  62. ?>

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