Code sharing for php cutting page div content. Highlights: 1. you can also use php to cut the page div. We hope that readers can provide more perfect solutions. 2. the cutting processing method has encapsulated highlights:
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.jb51.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.jb51.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
Tip 1. you can also use php to cut Page p. We hope that readers can provide more perfect solutions. 2. the cutting processing method has been encapsulated...