Highlight:
1, the use of PHP can also achieve the page div cutting processing. The practice here is to offer the reader a more perfect solution.
2, cutting processing method has been encapsulated into a method, can be directly referenced.
3, by the way, plus the tag cloud interception. Getwebdiv (' id= ' taglist "', ' http://www.jb51.net/tag/');
Copy CodeThe code is as follows:
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 ('/preg_match_all ('/<\/div/i ', $data, $suf _matches,preg_offset_capture);//Get all div suffixes
$hit = Strpos ($data, $div _id);
if ($hit = =-1) return false; Not hit
$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 ';
}
To sort a div
$sort = Array_keys ($divs);
Asort ($sort);
$count = count ($pre _matches[0]);
foreach ($pre _matches[0] as $index = = $pre _div) {
if (($pre _matches[0][$index][1] < $hit) && ($hit < $pre _matches[0][$index +1][1]) {
$deeper = 0;
Pop up the div before being hit Div
while (Array_shift ($sort)! = $pre _matches[0][$index][1] && ($count-)) continue;
The remaining div is matched, and if the next is prefixed, a layer down, $deeper plus 1,
Otherwise back one layer, $deeper minus 1, $deeper to 0 hit match, calculate div length
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). ";
Break
}
}
return $hitDivString;
}
echo getwebdiv (' id= ' taglist "', ' http://www.jb51.net/tag/');
end_php
Given the ID symbol problem, id= "U" is filled in by the user.
Disclaimer: This section of PHP is only for reads with the ID div content.
Perfect: Match any of the closed band ID tags
Copy CodeThe code is as follows:
View Code
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); Get all div suffixes
$hit = Strpos ($data, $tag _id);
if ($hit = =-1) return false; Not hit
$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 ';
}
To sort a div
$sort = Array_keys ($divs);
Asort ($sort);
$count = count ($pre _matches[0]);
foreach ($pre _matches[0] as $index = = $pre _div) {
if (($pre _matches[0][$index][1] < $hit) && ($hit < $pre _matches[0][$index +1][1]) {
$deeper = 0;
Pop up the div before being hit Div
while (Array_shift ($sort)! = $pre _matches[0][$index][1] && ($count-)) continue;
The remaining div is matched, and if the next is prefixed, a layer down, $deeper plus 1,
Otherwise back one layer, $deeper minus 1, $deeper to 0 hit match, calculate div length
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). ' ';
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
http://www.bkjia.com/PHPjc/325794.html www.bkjia.com true http://www.bkjia.com/PHPjc/325794.html techarticle Highlights: 1, the use of PHP can also achieve the page div cutting processing. The practice here is to offer the reader a more perfect solution. 2, cutting processing methods have been encapsulated ...