亮点:
1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。
2、切割处理方法已经封装成一个方法,可以直接引用。
3、顺便加上标签云的截取。//getWebDiv('id="taglist"','http://www.jb51.net/tag/');
复制代码 代码如下:
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('/
复制代码 代码如下:
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('/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){
//