php实现切割div内容的解决办法
内容摘要
这篇文章主要为大家详细介绍了php实现切割div内容的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!亮点:1、利用php也能
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!亮点:1、利用php也能
文章正文
这篇文章主要为大家详细介绍了php实现切割div内容的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随php教程的小玲来看看吧!
亮点:1、利用php也能实现对页面div的切割处理。这里的做法抛砖引玉,希望读者能够提供更加完美的解决方案。2、切割处理方法已经封装成一个方法,可以直接引用。3、顺便加上标签云的截取。//getWebDiv('id="taglist"','https://www.idcnote.com/tag/');代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <code class = "language-php" ><?php /* php教程 www.512Pic.com */ 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( '/<div/i' , $data , $pre_matches ,PREG_OFFSET_CAPTURE); //获取所有div前缀 preg_match_all( '/<\/div/i' , $data , $suf_matches ,PREG_OFFSET_CAPTURE); //获取所有div后缀 $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 ){ //<div $hit <div+1 时div被命中 if (( $pre_matches [0][ $index ][1] < $hit ) && ( $hit < $pre_matches [0][ $index +1][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 ). '</div>' ; break ; } } return $hitDivString ; } echo getWebDiv( 'id="taglist"' , 'https://www.idcnote.com/tag/' ); //End_php </code> |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <code class = "language-php" ><?php /* php教程 www.512Pic.com */ 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( '/<' . $tag . '/i' , $data , $pre_matches ,PREG_OFFSET_CAPTURE); //获取所有div前缀 preg_match_all( '/<\/' . $tag . '/i' , $data , $suf_matches ,PREG_OFFSET_CAPTURE); //获取所有div后缀 $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 ){ //<div $hit <div+1 时div被命中 if (( $pre_matches [0][ $index ][1] < $hit ) && ( $hit < $pre_matches [0][ $index +1][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 ). '</' . $tag . '>' ; break ; } } return $hitDivString ; } echo getWebTag( 'id="nav"' , 'http://mail.163.com/html/mail_intro/' , 'ul' ); echo getWebTag( 'id="homeBanners"' , 'http://mail.163.com/html/mail_intro/' ); echo getWebTag( 'id="performance"' , 'http://mail.163.com/html/mail_intro/' , 'section' ); //End_php </code> |
注:关于php实现切割div内容的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释