php cutting page div content implementation code sharing
- 2020-05-19 04:21:07
- OfStack
Window:
1. The page div can also be cut with php. We hope that readers can provide a more perfect solution.
2. The cutting processing method has been encapsulated into a method, which can be directly referenced.
3. Add the interception of the tag cloud by the way. / / getWebDiv (' id = "taglist" ', '/ / www. ofstack. com/tag/');
Considering the id symbol problem, id="u" is filled in by the user.
Declaration: this section of php is only for reading content with id div.
Perfect: matches any closed band id label
Author: Zjmainstay
1. The page div can also be cut with php. We hope that readers can provide a more perfect solution.
2. The cutting processing method has been encapsulated into a method, which can be directly referenced.
3. Add the interception of the tag cloud by the way. / / getWebDiv (' id = "taglist" ', '/ / www. ofstack. com/tag/');
<?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 the div The prefix
preg_match_all('/<\/div/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); // Get all the div The suffix
$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';
}
// right div sorting
$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 To be hit
if(($pre_matches[0][$index][1] < $hit) && ($hit < $pre_matches[0][$index+1][1])){
$deeper = 0;
// Ejection hit div In front of the div
while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue;
// The remaining div Match, if down 1 If a is a prefix, it goes down 1 Layer, $deeper add 1 .
// Otherwise the back 1 Layer, $deeper Reduction of 1 . $deeper for 0 Hit the match, calculate div The length of the
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"','//www.ofstack.com/tag/');
//End_php
Considering the id symbol problem, id="u" is filled in by the user.
Declaration: this section of php is only for reading content with id div.
Perfect: matches any closed band id label
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 the div The prefix
preg_match_all('/<\/'.$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); // Get all the div The suffix
$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';
}
// right div sorting
$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 To be hit
if(($pre_matches[0][$index][1] < $hit) && ($hit < $pre_matches[0][$index+1][1])){
$deeper = 0;
// Ejection hit div In front of the div
while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue;
// The remaining div Match, if down 1 If a is a prefix, it goes down 1 Layer, $deeper add 1 .
// Otherwise the back 1 Layer, $deeper Reduction of 1 . $deeper for 0 Hit the match, calculate div The length of the
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