Implementation code sharing of php cutting page div content_PHP tutorial

WBOY
Release: 2016-07-21 15:17:13
Original
859 people have browsed it

Highlights:
1. You can also use PHP to cut page divs. The approach here is a starting point, and I hope readers can provide more perfect solutions.
2. The cutting processing method has been encapsulated into a method and can be quoted directly.
3. Add tag cloud interception by the way. //getWebDiv('id="taglist"','http://www.jb51.net/tag/');

Copy code The 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('/
$hit = strpos($data,$div_id);
if($hit == -1) return false; //Missed
$divs = array( ); //Merge all divs
foreach($pre_matches[0] as $index=>$pre_div){
$divs[(int)$pre_div[1]] = 'p';
$divs[(int)$suf_matches[0][$index][1]] = 's';
}
//Sort divs
$sort = array_keys($divs);
asort($sort);
$count = count($pre_matches[0]);
foreach($pre_matches[0] as $index=>$pre_div){
//< div $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 divs, if the next one is a prefix, go one level down, $deeper adds 1,
//Otherwise go back one level, $deeper is reduced by 1. If $deeper is 0, the match is hit. Calculate the 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

Considering the issue of id symbols, id="u" is filled in by the user themselves.
Statement: This php section is only for reading the content of div with id.
Perfect: Match any closeable tag with id
Copy code The 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); //获取所有div前缀
preg_match_all('/$hit = strpos($data,$tag_id);
if($hit == -1) return false; //未命中
$divs = array(); //合并所有div
foreach($pre_matches[0] as $index=>$pre_div){
$divs[(int)$pre_div[1]] = 'p';
$divs[(int)$suf_matches[0][$index][1]] = 's';
}
//对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;
//弹出被命中div前的div
while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue;
//对剩余div进行匹配,若下一个为前缀,则向下一层,$deeper加1,
//否则后退一层,$deeper减1,$deeper为0则命中匹配,计算div长度
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

作者: Zjmainstay

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/325794.htmlTechArticle亮点: 1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。 2、切割处理方法已经封装...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!