A phpQuery-based php common collection class sharing _ PHP Tutorial

Source: Internet
Author: User
A phpQuery-based php common collection class sharing. I. Sample source code copy code :? Php *** Common list collection class * V1.3 * author: JAE * require_once .. phpQueryphpQueryphpQuery. php; classQueryList {priva I. Collection Source code

The code is as follows:


/**
* General list Collection
* Version V1.3
* Author: JAE
*/
Require_once '../phpQuery. php ';
Class QueryList {

Private $ pageURL;
Private $ regArr = array ();
Public $ jsonArr = array ();
Private $ regRange;
Private $ html;
/*************************************** *********
* Parameter: page address selector array block selector
* [Selector array] description: format array ("name" => array ("selector", "type "),.......)
* [Type] description: value "text", "html", "attribute"
* [Block selector]: selects several blocks according to the rules, and then selects the blocks separately.
**************************************** *********/
Function QueryList ($ pageURL, $ regArr = array (), $ regRange = '')
{
$ This-> pageURL = $ pageURL;

// To obtain https ://
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ this-> pageURL );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
$ This-> html = curl_exec ($ ch );
Curl_close ($ ch );

If (! Empty ($ regArr ))
{

$ This-> regArr = $ regArr;
$ This-> regRange = $ regRange;
$ This-> getList ();
}

}
Function setQuery ($ regArr, $ regRange = '')
{
$ This-> jsonArr = array ();
$ This-> regArr = $ regArr;
$ This-> regRange = $ regRange;
$ This-> getList ();
}
Private function getList ()
{

$ Hobj = phpQuery: newjavasenthtml ($ this-> html );
If (! Empty ($ this-> regRange ))
{
$ Robj = pq ($ hobj)-> find ($ this-> regRange );

$ I = 0;
Foreach ($ robj as $ item)
{

While (list ($ key, $ reg_value) = each ($ this-> regArr ))
{
$ Iobj = pq ($ item)-> find ($ reg_value [0]);

Switch ($ reg_value [1])
{
Case 'text ':
$ This-> jsonArr [$ I] [$ key] = trim (pq ($ iobj)-> text ());
Break;
Case 'html ':
$ This-> jsonArr [$ I] [$ key] = trim (pq ($ iobj)-> html ());
Break;
Default:
$ This-> jsonArr [$ I] [$ key] = pq ($ iobj)-> attr ($ reg_value [1]);
Break;

}
}
// Reset the array pointer
Reset ($ this-> regArr );
$ I ++;
}
}
Else
{
While (list ($ key, $ reg_value) = each ($ this-> regArr ))
{
$ Lobj = pq ($ hobj)-> find ($ reg_value [0]);


$ I = 0;
Foreach ($ lobj as $ item)
{
Switch ($ reg_value [1])
{
Case 'text ':
$ This-> jsonArr [$ I ++] [$ key] = trim (pq ($ item)-> text ());
Break;
Case 'html ':
$ This-> jsonArr [$ I ++] [$ key] = trim (pq ($ item)-> html ());
Break;
Default:
$ This-> jsonArr [$ I ++] [$ key] = pq ($ item)-> attr ($ reg_value [1]);
Break;

}


}


}
}
}
Function getJSON ()
{
Return json_encode ($ this-> jsonArr );
}

}

II. example

The code is as follows:


Require 'query/QueryList. class. php ';


// Collect the OSC code sharing list, with the title linked to the Author
$ Url = "http://www.oschina.net/code/list ";
$ Reg = array ("title" => array (". code_title a: eq (0) "," text ")," url "=> array (". code_title a: eq (0) "," href ")," author "=> array (" img "," title "));
$ Rang = ". code_list li ";
$ Hj = new QueryList ($ url, $ reg, $ rang );
$ Arr = $ hj-> jsonArr;
Print_r ($ arr );
// If you want to obtain the top 40 active contributor images on the right of the current page and obtain JSON data, you can write
$ Reg = array ("portrait" => array (". hot_top img", "src "));
$ Hj-> setQuery ($ reg );
$ Json = $ hj-> getJSON ();
Echo $ json ."

";

// Obtain the content on the OSC content page
$ Url = "http://www.oschina.net/code/snippet_186288_23816 ";
$ Reg = array ("title" => array (". QTitle h1 "," text ")," con "=> array (". content "," html "));
$ Hj = new QueryList ($ url, $ reg );
$ Arr = $ hj-> jsonArr;
Print_r ($ arr );

// Let's take so many examples. is it very convenient to collect data?

The pipeline code is as follows :? Php/*** Common list collection class * V1.3 * author: JAE */require_once '../phpQuery. php'; class QueryList {priva...

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.