Rumah > php教程 > php手册 > teks badan

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

WBOY
Lepaskan: 2016-06-06 19:47:18
asal
1078 orang telah melayarinya

亮点: 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="/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" alt="php获取页面并切割页面div内容" ><img  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" 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
Salin selepas log masuk
.'/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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan