A php text collection class-php Tutorial

Source: Internet
Author: User
A php text collection class

  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 ';
  3. $ C-> startFlag ='

    ';
  4. $ C-> endFlag ='

    ';

  5. $ C-> init ();
  6. $ C-> regExp = "|

    (.*)

    (.*)

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

Php text collection files:

  1. <%

  2. /**
  3. Module name: php text collection class
  4. **/
  5. Class Collection {
  6. // Public portal
  7. Var $ url; // url to be analyzed
  8. Var $ content; // read content
  9. Var $ regExp; // the regular expression of the part to be obtained
  10. Var $ codeFrom; // encoding of the original text
  11. Var $ codeTo; // encoding to be converted
  12. Var $ timeout; // Collection wait time

  13. Var $ startFlag; // The marker that is collected from the document is 0 by default. when collecting entries, only text blocks between $ startFlag and $ endFlag are searched and collected.

  14. Var $ endFlag; // the marker for collecting end-of-article data. by default, only text blocks between $ startFlag and $ endFlag are searched and collected at the end of the article.
  15. Var $ block; // text block between $ startFlag and $ endFlag
  16. // Export private
  17. Var $ result; // output result

  18. // Initialize the collector

  19. Function init (){
  20. If (empty ($ url ))
  21. $ This-> getFile ();
  22. $ This-> convertEncoding ();
  23. }
  24. // Collect 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. // Handle errors
  31. Function error ($ msg ){
  32. Echo $ msg;
  33. }
  34. // If the remote webpage is successfully read, the system returns the object; if the remote webpage fails, the system returns false.
  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. // Obtain the required text block
  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. // Content encoding conversion
  54. Function convertEncoding (){
  55. If (! Empty ($ this-> codeTo ))
  56. $ This-> codeFrom = mb_detect_encoding ($ this-> content );
  57. // If a conversion scheme is specified, the conversion is executed.
  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. ?>

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.