php获取页面并切割页面div内容
亮点: 1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。 2、切割处理方法已经封装成一个方法,可以直接引用。 3、顺便加上的截
亮点:
1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。
2、切割处理方法已经封装成一个方法,可以直接引用。
3、顺便加上的截取。//getWebDiv('id="taglist"','http://www.cnblogs.com/Zjmainstay/tag/');
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
修复:stripos($data,'charset=utf-8',$charset_pos) 加入charset=,避免有些gb2312格式的网页中包含utf-8造成错误。或者用户可以自行修改函数传入一个确定的charset参数。
演示地址:parseDiv

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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,

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

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

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 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 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.
