search
Homephp教程php手册php切割页面div内容的实现代码

今天在百度知道看到一个关于php获取DIV内容的问题,做了一晚,终于是做出来了

复制代码 代码如下:
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; //未命中
$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] $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 getWebDiv('id="u"','http://www.baidu.com');
//End_php

考虑到id符号问题,id="u"由用户自己填写。
声明:此段php只针对带 id div内容的读取。
作者: Zjmainstay
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!