Today, Baidu saw a problem about getting DIV content from php. After one night, it was finally made.
Today, Baidu saw a problem about getting DIV content from php. After one night, it was finally made.
The Code is as follows:
Header ("Content-type: text/html; charset = UTF-8 ");
Function getWebDiv ($ p_id, $ url = false, $ data = false ){
If ($ url! = False ){
$ Data = file_get_contents ($ url );
}
$ Charset_pos = stripos ($ data, 'charset ');
If ($ charset_pos ){
If (stripos ($ data, 'utf-8', $ charset_pos )){
$ Data = iconv ('utf-8', 'utf-8', $ data );
} Else if (stripos ($ data, 'gb2312', $ charset_pos )){
$ Data = iconv ('gb2312', 'utf-8', $ data );
} Else if (stripos ($ data, 'gbk', $ charset_pos )){
$ Data = iconv ('gbk', 'utf-8', $ data );
}
}
Preg_match_all ('/
Preg_match_all ('/<\/p/I', $ data, $ suf_matches, PREG_OFFSET_CAPTURE); // obtain all p suffixes
$ Hit = strpos ($ data, $ p_id );
If ($ hit =-1) return false; // missed
$ Ps = array (); // merge all p
Foreach ($ pre_matches [0] as $ index => $ pre_p ){
$ Ps [(int) $ pre_p [1] = 'P ';
$ Ps [(int) $ suf_matches [0] [$ index] [1] ='s ';
}
// Sort p
$ Sort = array_keys ($ ps );
Asort ($ sort );
$ Count = count ($ pre_matches [0]);
Foreach ($ pre_matches [0] as $ index => $ pre_p ){
//
If ($ pre_matches [0] [$ index] [1] <$ hit) & ($ hit <$ pre_matches [0] [$ index + 1] [1]) {
$ Deeper = 0;
// Pop up the p before the hit p
While (array_shift ($ sort )! = $ Pre_matches [0] [$ index] [1] & ($ count --) continue;
// Match the remaining p. If the next one is the prefix, add 1 to the next layer, $ deeper,
// Otherwise, a layer is left behind. $ deeper minus 1. If $ deeper is 0, a match is hit. The length of p is calculated.
Foreach ($ sort as $ key ){
If ($ ps [$ key] = 'P') $ deeper ++;
Else if ($ deeper = 0 ){
$ Length = $ key-$ pre_matches [0] [$ index] [1];
Break;
} Else {
$ Deeper --;
}
}
$ HitDivString = substr ($ data, $ pre_matches [0] [$ index] [1], $ length ).'
';
Break;
}
}
Return $ hitDivString;
}
Echo getWebDiv ('Id = "u" ', 'HTTP: // www.baidu.com ');
// End_php
Considering the id symbol, id = "u" is filled by the user.
Declaration: This php segment is only for reading content with id p.
Author: Zjmainstay