Php cutting page div content implementation code sharing _ php instance

Source: Internet
Author: User
Today, Baidu saw a problem about getting DIV content from php. After one night, it finally made a bright spot:
1. You can use php to cut page p. We hope that readers can provide more perfect solutions.
2. The cut processing method has been encapsulated into a method and can be directly referenced.
3. Tag Cloud interception. // GetWebDiv ('Id = "taglist" ', 'HTTP: // www.php.net/tag /');

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 = "taglist" ', 'HTTP: // www.php.net/tag /');
// 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.
Perfect: matching any tags with id that can be closed

The Code is as follows:


View Code
Header ("Content-type: text/html; charset = UTF-8 ");
Function getWebTag ($ tag_id, $ url = false, $ tag = 'P', $ 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 ('/<'. $ tag. '/I', $ data, $ pre_matches, PREG_OFFSET_CAPTURE); // obtain all p prefixes
Preg_match_all ('/<\/'. $ tag. '/I', $ data, $ suf_matches, PREG_OFFSET_CAPTURE); // obtain all p suffixes
$ Hit = strpos ($ data, $ tag_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 getWebTag ('Id = "nav" ', 'HTTP: // mail.163.com/html/mail_intro/', 'ul ');
Echo getWebTag ('Id = "homeBanners" ', 'HTTP: // mail.163.com/html/mail_intro /');
Echo getWebTag ('Id = "performance" ', 'HTTP: // mail.163.com/html/mail_intro/', 'section ');
// End_php


Author: Zjmainstay

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.