zoukankan      html  css  js  c++  java
  • php获取页面指定标签内容的实现代码分享

    php获取页面指定标签内容的实现代码分享
    可以匹配任意可闭合带id标签

    <?php
    header ( "Content-type: text/html; charset=utf-8" );

    /*
    * 参数说明: $tag_id:所要获取的元素Tag Id $url:所要获取页面的Url $tag:所要获取的标签 $data
    */
    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;
    }

    //test
    echo getWebTag ( 'id="content"', 'http://www.cnblogs.com/suizhikuo/archive/2012/11/17/2774956.html', 'div' );
    ?>

  • 相关阅读:
    结合源码浅析Struts2与Spring整合的原理
    LINUX centOS6.x下安装redis
    基于Spring注解@cacheable 集成redis
    windows下搭建LDAP并利用Java实现对LDAP的操作
    Java利用freemaker和(excelXML表格或wordXML表格),导出自己任何想要格式的文档
    创建oracle表的时候一个小细节,会导致你处理java类型转换是时候很麻烦
    socketlog的安装和使用
    Windows 定时任务对数据库进行操作
    将博客搬至CSDN
    git+gitlab实现git版本控制管理本地化+自动化部署
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/2777504.html
Copyright © 2011-2022 走看看