Home php教程 php手册 php获取页面并切割页面div内容

php获取页面并切割页面div内容

Jun 06, 2016 pm 07:47 PM
div php Highlights content cutting use Obtain page

亮点: 1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。 2、切割处理方法已经封装成一个方法,可以直接引用。 3、顺便加上的截

亮点:

      1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。

      2、切割处理方法已经封装成一个方法,可以直接引用。

      3、顺便加上的截取。//getWebDiv('id="taglist"','http://www.cnblogs.com/Zjmainstay/tag/');

php获取页面并切割页面div内容php获取页面并切割页面div内容View Code

<span>php
    </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>); 
    </span><span>function</span> getWebDiv(<span>$div_id</span>,<span>$url</span>=<span>false</span>,<span>$data</span>=<span>false</span><span>){
        </span><span>if</span>(<span>$url</span> !== <span>false</span><span>){
            </span><span>$data</span> = <span>file_get_contents</span>( <span>$url</span><span> );
        }
        </span><span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>);
        </span><span>if</span>(<span>$charset_pos</span><span>) {
            </span><span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=utf-8',<span>$charset_pos</span><span>)) {
                </span><span>$data</span> = <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gb2312',<span>$charset_pos</span><span>)) {
                </span><span>$data</span> = <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>);
            }</span><span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gbk',<span>$charset_pos</span><span>)) {
                </span><span>$data</span> = <span>iconv</span>('gbk','utf-8',<span>$data</span><span>);
            }
        }
        
        </span><span>preg_match_all</span>('/<div>$data,<span>$pre_matches</span>,PREG_OFFSET_CAPTURE);    <span>//</span><span>获取所有div前缀</span>
        <span>preg_match_all</span>('/$data,<span>$suf_matches</span>,PREG_OFFSET_CAPTURE); <span>//</span><span>获取所有div后缀</span>
        <span>$hit</span> = <span>strpos</span>(<span>$data</span>,<span>$div_id</span><span>);
        </span><span>if</span>(<span>$hit</span> == -1) <span>return</span> <span>false</span>;    <span>//</span><span>未命中</span>
        <span>$divs</span> = <span>array</span>();    <span>//</span><span>合并所有div</span>
        <span>foreach</span>(<span>$pre_matches</span>[0] <span>as</span> <span>$index</span>=><span>$pre_div</span><span>){
            </span><span>$divs</span>[(int)<span>$pre_div</span>[1]] = 'p'<span>;
            </span><span>$divs</span>[(int)<span>$suf_matches</span>[0][<span>$index</span>][1]] = 's'<span>;    
        }
        
        </span><span>//</span><span>对div进行排序</span>
        <span>$sort</span> = <span>array_keys</span>(<span>$divs</span><span>);
        </span><span>asort</span>(<span>$sort</span><span>);
        
        </span><span>$count</span> = <span>count</span>(<span>$pre_matches</span>[0<span>]);
        </span><span>foreach</span>(<span>$pre_matches</span>[0] <span>as</span> <span>$index</span>=><span>$pre_div</span><span>){
            </span><span>//</span><span><div>
            <span>if</span>((<span>$pre_matches</span>[0][<span>$index</span>][1] $hit) && (<span>$hit</span> $pre_matches[0][<span>$index</span>+1][1<span>])){
                </span><span>$deeper</span> = 0<span>;
                </span><span>//</span><span>弹出被命中div前的div</span>
                <span>while</span>(<span>array_shift</span>(<span>$sort</span>) != <span>$pre_matches</span>[0][<span>$index</span>][1] && (<span>$count</span>--)) <span>continue</span><span>;
                </span><span>//</span><span>对剩余div进行匹配,若下一个为前缀,则向下一层,$deeper加1,
                //否则后退一层,$deeper减1,$deeper为0则命中匹配,计算div长度</span>
                <span>foreach</span>(<span>$sort</span> <span>as</span> <span>$key</span><span>){
                    </span><span>if</span>(<span>$divs</span>[<span>$key</span>] == 'p') <span>$deeper</span>++<span>;
                    </span><span>else</span> <span>if</span>(<span>$deeper</span> == 0<span>) {
                        </span><span>$length</span> = <span>$key</span>-<span>$pre_matches</span>[0][<span>$index</span>][1<span>];
                        </span><span>break</span><span>;
                    }</span><span>else</span><span> {
                        </span><span>$deeper</span>--<span>;
                    }
                }
                </span><span>$hitDivString</span> = <span>substr</span>(<span>$data</span>,<span>$pre_matches</span>[0][<span>$index</span>][1],<span>$length</span>).'</div>'<span>;
                </span><span>break</span><span>;
            }
        }
        </span><span>return</span> <span>$hitDivString</span><span>;
    }
    
    </span><span>echo</span> getWebDiv('id="taglist"','http://www.cnblogs.com/Zjmainstay/tag/'<span>);

</span><span>//</span><span>End_php</span>


<p>考虑到id符号问题,id="u"由用户自己填写。</p>
<p>声明:此段php只针对带 id div内容的读取。</p>

<p><img  src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnblogs.com%2FOutliningIndicators%2FContractedBlock.gif&refer=http%3A%2F%2Fwww.cnblogs.com%2FZjmainstay%2Farchive%2F2012%2F08%2F06%2Fphp_getDivContain.html" class="lazy" alt="php获取页面并切割页面div内容" ><img  src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnblogs.com%2FOutliningIndicators%2FExpandedBlockStart.gif&refer=http%3A%2F%2Fwww.cnblogs.com%2FZjmainstay%2Farchive%2F2012%2F08%2F06%2Fphp_getDivContain.html" class="lazy" alt="php获取页面并切割页面div内容" ><span>View Code </span>
</p>
<p>
</p>
<pre class="brush:php;toolbar:false"><span> 1</span> <span>php
</span><span> 2</span>     <span>header</span>("Content-type: text/html; charset=utf-8"<span>); 
</span><span> 3</span>     <span>function</span> getWebTag(<span>$tag_id</span>,<span>$url</span>=<span>false</span>,<span>$tag</span>='div',<span>$data</span>=<span>false</span><span>){
</span><span> 4</span>         <span>if</span>(<span>$url</span> !== <span>false</span><span>){
</span><span> 5</span>             <span>$data</span> = <span>file_get_contents</span>( <span>$url</span><span> );
</span><span> 6</span> <span>        }
</span><span> 7</span>         <span>$charset_pos</span> = <span>stripos</span>(<span>$data</span>,'charset'<span>);
</span><span> 8</span>         <span>if</span>(<span>$charset_pos</span><span>) {
</span><span> 9</span>             <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=utf-8',<span>$charset_pos</span><span>)) {
</span><span>10</span>                 <span>$data</span> = <span>iconv</span>('utf-8','utf-8',<span>$data</span><span>);
</span><span>11</span>             }<span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gb2312',<span>$charset_pos</span><span>)) {
</span><span>12</span>                 <span>$data</span> = <span>iconv</span>('gb2312','utf-8',<span>$data</span><span>);
</span><span>13</span>             }<span>else</span> <span>if</span>(<span>stripos</span>(<span>$data</span>,'charset=gbk',<span>$charset_pos</span><span>)) {
</span><span>14</span>                 <span>$data</span> = <span>iconv</span>('gbk','utf-8',<span>$data</span><span>);
</span><span>15</span> <span>            }
</span><span>16</span> <span>        }
</span><span>17</span>         
<span>18</span>         <span>preg_match_all</span>('/$tag
Copy after login
.'/i',$data,$pre_matches,PREG_OFFSET_CAPTURE); //获取所有div前缀 19 preg_match_all('/$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); //获取所有div后缀 20 $hit = strpos($data,$tag_id); 21 if($hit == -1) return false; //未命中 22 $divs = array(); //合并所有div 23 foreach($pre_matches[0] as $index=>$pre_div){ 24 $divs[(int)$pre_div[1]] = 'p'; 25 $divs[(int)$suf_matches[0][$index][1]] = 's'; 26 } 27 28 //对div进行排序 29 $sort = array_keys($divs); 30 asort($sort); 31 32 $count = count($pre_matches[0]); 33 foreach($pre_matches[0] as $index=>$pre_div){ 34 //
35 if(($pre_matches[0][$index][1] $hit) && ($hit $pre_matches[0][$index+1][1])){ 36 $deeper = 0; 37 //弹出被命中div前的div 38 while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue; 39 //对剩余div进行匹配,若下一个为前缀,则向下一层,$deeper加1, 40 //否则后退一层,$deeper减1,$deeper为0则命中匹配,计算div长度 41 foreach($sort as $key){ 42 if($divs[$key] == 'p') $deeper++; 43 else if($deeper == 0) { 44 $length = $key-$pre_matches[0][$index][1]; 45 break; 46 }else { 47 $deeper--; 48 } 49 } 50 $hitDivString = substr($data,$pre_matches[0][$index][1],$length).''.$tag.'>'; 51 break; 52 } 53 } 54 return $hitDivString; 55 } 56 57 echo getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul'); 58 echo getWebTag('id="homeBanners"','http://mail.163.com/html/mail_intro/'); 59 echo getWebTag('id="performance"','http://mail.163.com/html/mail_intro/','div'); 60 61 //End_php

 

修复:stripos($data,'charset=utf-8',$charset_pos) 加入charset=,避免有些gb2312格式的网页中包含utf-8造成错误。或者用户可以自行修改函数传入一个确定的charset参数。

演示地址:parseDiv

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles