Php cutting page div content implementation code sharing

Source: Internet
Author: User

Highlights:
1. You can also use php to cut the page div. 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 /'); Copy codeThe Code is as follows: <? Php
Header ("Content-type: text/html; charset = UTF-8 ");
Function getWebDiv ($ div_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 ('/<div/I', $ data, $ pre_matches, PREG_OFFSET_CAPTURE); // get all div prefixes
Preg_match_all ('/<\/div/I', $ data, $ suf_matches, PREG_OFFSET_CAPTURE); // obtain all div suffixes
$ Hit = strpos ($ data, $ div_id );
If ($ hit =-1) return false; // missed
$ Divs = array (); // merge all div
Foreach ($ pre_matches [0] as $ index => $ pre_div ){
$ Divs [(int) $ pre_div [1] = 'P ';
$ Divs [(int) $ suf_matches [0] [$ index] [1] ='s ';
}
// Sort the div
$ Sort = array_keys ($ divs );
Asort ($ sort );
$ Count = count ($ pre_matches [0]);
Foreach ($ pre_matches [0] as $ index => $ pre_div ){
// <Div $ hit <div + 1 when div is hit
If ($ pre_matches [0] [$ index] [1] <$ hit) & ($ hit <$ pre_matches [0] [$ index + 1] [1]) {
$ Deeper = 0;
// Pop up the div before the hit div
While (array_shift ($ sort )! = $ Pre_matches [0] [$ index] [1] & ($ count --) continue;
// Match the remaining div. If the next one is the prefix, the next layer will be added to $ deeper with 1,
// Otherwise, a layer is left behind. $ deeper minus 1. If $ deeper is 0, matching is hit. The div length is calculated.
Foreach ($ sort as $ key ){
If ($ divs [$ 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). '</div> ';
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 div.
Perfect: matching any tags with id that can be closedCopy codeThe Code is as follows: View Code
<? Php
Header ("Content-type: text/html; charset = UTF-8 ");
Function getWebTag ($ tag_id, $ url = false, $ tag = 'div ', $ 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); // get all div prefixes
Preg_match_all ('/<\/'. $ tag. '/I', $ data, $ suf_matches, PREG_OFFSET_CAPTURE); // obtain all div suffixes
$ Hit = strpos ($ data, $ tag_id );
If ($ hit =-1) return false; // missed
$ Divs = array (); // merge all div
Foreach ($ pre_matches [0] as $ index => $ pre_div ){
$ Divs [(int) $ pre_div [1] = 'P ';
$ Divs [(int) $ suf_matches [0] [$ index] [1] ='s ';
}
// Sort the div
$ Sort = array_keys ($ divs );
Asort ($ sort );
$ Count = count ($ pre_matches [0]);
Foreach ($ pre_matches [0] as $ index => $ pre_div ){
// <Div $ hit <div + 1 when div is hit
If ($ pre_matches [0] [$ index] [1] <$ hit) & ($ hit <$ pre_matches [0] [$ index + 1] [1]) {
$ Deeper = 0;
// Pop up the div before the hit div
While (array_shift ($ sort )! = $ Pre_matches [0] [$ index] [1] & ($ count --) continue;
// Match the remaining div. If the next one is the prefix, the next layer will be added to $ deeper with 1,
// Otherwise, a layer is left behind. $ deeper minus 1. If $ deeper is 0, matching is hit. The div length is calculated.
Foreach ($ sort as $ key ){
If ($ divs [$ 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). '</'. $ tag. '> ';
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.