Skip to content
컴퓨터잡담
2011.09.02 07:14

[php] Htmlparser.inc, Htmlparser.php Dom Paser

조회 수 5245 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부




http://sourceforge.net/projects/php-html/files/


phphtmlparser_1_0.tar.gz Htmlparser.php Htmlparser.inc


Content:

Htmlparser.inc:

<?php 

/* 
* Copyright (c) 2003 Jose Solorzano.  All rights reserved. 
* Redistribution of source must retain this copyright notice. 

* Jose Solorzano (http://jexpert.us) is a software consultant. 

* Contributions by: 
* - Leo West (performance improvements) 
*/ 

define ("NODE_TYPE_START", 0); 
define ("NODE_TYPE_ELEMENT", 1); 
define ("NODE_TYPE_ENDELEMENT", 2); 
define ("NODE_TYPE_TEXT", 3); 
define ("NODE_TYPE_COMMENT", 4); 
define ("NODE_TYPE_DONE", 5); 

/** 
* Class HtmlParser. 
* To use, create an instance of the class passing 
* HTML text. Then invoke parse() until it's false. 
* When parse() returns true, $iNodeType, $iNodeName 
* $iNodeValue and $iNodeAttributes are updated. 

* To create an HtmlParser instance you may also 
* use convenience functions HtmlParser_ForFile 
* and HtmlParser_ForURL. 
*/ 
class HtmlParser { 

    /** 
    * Field iNodeType. 
    * May be one of the NODE_TYPE_* constants above. 
    */ 
    var $iNodeType; 

    /** 
    * Field iNodeName. 
    * For elements, it's the name of the element. 
    */ 
    var $iNodeName = ""; 

    /** 
    * Field iNodeValue. 
    * For text nodes, it's the text. 
    */ 
    var $iNodeValue = ""; 

    /** 
    * Field iNodeAttributes. 
    * A string-indexed array containing attribute values 
    * of the current node. Indexes are always lowercase. 
    */ 
    var $iNodeAttributes; 

    // The following fields should be 
    // considered private: 

    var $iHtmlText; 
    var $iHtmlTextLength; 
    var $iHtmlTextIndex = 0; 
    var $iHtmlCurrentChar; 
    var $BOE_ARRAY; 
    var $B_ARRAY; 
    var $BOS_ARRAY; 
    
    /** 
    * Constructor. 
    * Constructs an HtmlParser instance with 
    * the HTML text given. 
    */ 
    function HtmlParser ($aHtmlText) { 
        $this->iHtmlText = $aHtmlText; 
        $this->iHtmlTextLength = strlen($aHtmlText); 
        $this->iNodeAttributes = array(); 
        $this->setTextIndex (0); 

        $this->BOE_ARRAY = array (" ", "\t", "\r", "\n", "=" ); 
        $this->B_ARRAY = array (" ", "\t", "\r", "\n" ); 
        $this->BOS_ARRAY = array (" ", "\t", "\r", "\n", "/" ); 
    } 

    /** 
    * Method parse. 
    * Parses the next node. Returns false only if 
    * the end of the HTML text has been reached. 
    * Updates values of iNode* fields. 
    */ 
    function parse() { 
        $text = $this->skipToElement(); 
        if ($text != "") { 
            $this->iNodeType = NODE_TYPE_TEXT; 
            $this->iNodeName = "Text"; 
            $this->iNodeValue = $text; 
            return true; 
        } 
        return $this->readTag(); 
    } 

    function clearAttributes() { 
        $this->iNodeAttributes = array(); 
    } 

    function readTag() { 
        if ($this->iCurrentChar != "<") { 
            $this->iNodeType = NODE_TYPE_DONE; 
            return false; 
        } 
        $this->clearAttributes(); 
        $this->skipMaxInTag ("<", 1); 
        if ($this->iCurrentChar == '/') { 
            $this->moveNext(); 
            $name = $this->skipToBlanksInTag(); 
            $this->iNodeType = NODE_TYPE_ENDELEMENT; 
            $this->iNodeName = $name; 
            $this->iNodeValue = "";            
            $this->skipEndOfTag(); 
            return true; 
        } 
        $name = $this->skipToBlanksOrSlashInTag(); 
        if (!$this->isValidTagIdentifier ($name)) { 
                $comment = false; 
                if (strpos($name, "!--") === 0) { 
                    $ppos = strpos($name, "--", 3); 
                    if (strpos($name, "--", 3) === (strlen($name) - 2)) { 
                        $this->iNodeType = NODE_TYPE_COMMENT; 
                        $this->iNodeName = "Comment"; 
                        $this->iNodeValue = "<" . $name . ">"; 
                        $comment = true;                        
                    } 
                    else { 
                        $rest = $this->skipToStringInTag ("-->");    
                        if ($rest != "") { 
                            $this->iNodeType = NODE_TYPE_COMMENT; 
                            $this->iNodeName = "Comment"; 
                            $this->iNodeValue = "<" . $name . $rest; 
                            $comment = true; 
                            // Already skipped end of tag 
                            return true; 
                        } 
                    } 
                } 
                if (!$comment) { 
                    $this->iNodeType = NODE_TYPE_TEXT; 
                    $this->iNodeName = "Text"; 
                    $this->iNodeValue = "<" . $name; 
                    return true; 
                } 
        } 
        else { 
                $this->iNodeType = NODE_TYPE_ELEMENT; 
                $this->iNodeValue = ""; 
                $this->iNodeName = $name; 
                while ($this->skipBlanksInTag()) { 
                    $attrName = $this->skipToBlanksOrEqualsInTag(); 
                    if ($attrName != "" && $attrName != "/") { 
                        $this->skipBlanksInTag(); 
                        if ($this->iCurrentChar == "=") { 
                            $this->skipEqualsInTag(); 
                            $this->skipBlanksInTag(); 
                            $value = $this->readValueInTag(); 
                            $this->iNodeAttributes[strtolower($attrName)] = $value; 
                        } 
                        else { 
                            $this->iNodeAttributes[strtolower($attrName)] = ""; 
                        } 
                    } 
                } 
        } 
        $this->skipEndOfTag(); 
        return true;            
    } 

    function isValidTagIdentifier ($name) { 
        return ereg ("^[A-Za-z0-9_\\-]+$", $name); 
    } 
    
    function skipBlanksInTag() { 
        return "" != ($this->skipInTag ($this->B_ARRAY)); 
    } 

    function skipToBlanksOrEqualsInTag() { 
        return $this->skipToInTag ($this->BOE_ARRAY); 
    } 

    function skipToBlanksInTag() { 
        return $this->skipToInTag ($this->B_ARRAY); 
    } 

    function skipToBlanksOrSlashInTag() { 
        return $this->skipToInTag ($this->BOS_ARRAY); 
    } 

    function skipEqualsInTag() { 
        return $this->skipMaxInTag ("=", 1); 
    } 

    function readValueInTag() { 
        $ch = $this->iCurrentChar; 
        $value = ""; 
        if ($ch == "\"") { 
            $this->skipMaxInTag ("\"", 1); 
            $value = $this->skipToInTag ("\""); 
            $this->skipMaxInTag ("\"", 1); 
        } 
        else if ($ch == "'") { 
            $this->skipMaxInTag ("'", 1); 
            $value = $this->skipToInTag ("'"); 
            $this->skipMaxInTag ("'", 1); 
        }                
        else { 
            $value = $this->skipToBlanksInTag(); 
        } 
        return $value; 
    } 

    function setTextIndex ($index) { 
        $this->iHtmlTextIndex = $index; 
        if ($index >= $this->iHtmlTextLength) { 
            $this->iCurrentChar = -1; 
        } 
        else { 
            $this->iCurrentChar = $this->iHtmlText{$index}; 
        } 
    } 

    function moveNext() { 
        if ($this->iHtmlTextIndex < $this->iHtmlTextLength) { 
            $this->setTextIndex ($this->iHtmlTextIndex + 1); 
            return true; 
        } 
        else { 
            return false; 
        } 
    } 

    function skipEndOfTag() { 
        while (($ch = $this->iCurrentChar) !== -1) { 
            if ($ch == ">") { 
                $this->moveNext(); 
                return; 
            } 
            $this->moveNext(); 
        } 
    } 

    function skipInTag ($chars) { 
        $sb = ""; 
        while (($ch = $this->iCurrentChar) !== -1) { 
            if ($ch == ">") { 
                return $sb; 
            } else { 
                $match = false; 
                for ($idx = 0; $idx < count($chars); $idx++) { 
                    if ($ch == $chars[$idx]) { 
                        $match = true; 
                        break; 
                    } 
                } 
                if (!$match) { 
                    return $sb; 
                } 
                $sb .= $ch; 
                $this->moveNext(); 
            } 
        } 
        return $sb; 
    } 

    function skipMaxInTag ($chars, $maxChars) { 
        $sb = ""; 
        $count = 0; 
        while (($ch = $this->iCurrentChar) !== -1 && $count++ < $maxChars) { 
            if ($ch == ">") { 
                return $sb; 
            } else { 
                $match = false; 
                for ($idx = 0; $idx < count($chars); $idx++) { 
                    if ($ch == $chars[$idx]) { 
                        $match = true; 
                        break; 
                    } 
                } 
                if (!$match) { 
                    return $sb; 
                } 
                $sb .= $ch; 
                $this->moveNext(); 
            } 
        } 
        return $sb; 
    } 

    function skipToInTag ($chars) { 
        $sb = ""; 
        while (($ch = $this->iCurrentChar) !== -1) { 
            $match = $ch == ">"; 
            if (!$match) { 
                for ($idx = 0; $idx < count($chars); $idx++) { 
                    if ($ch == $chars[$idx]) { 
                        $match = true; 
                        break; 
                    } 
                } 
            } 
            if ($match) { 
                return $sb; 
            } 
            $sb .= $ch; 
            $this->moveNext(); 
        } 
        return $sb; 
    } 

    function skipToElement() { 
        $sb = ""; 
        while (($ch = $this->iCurrentChar) !== -1) { 
            if ($ch == "<") { 
                return $sb; 
            } 
            $sb .= $ch; 
            $this->moveNext(); 
        } 
        return $sb;            
    } 

    /** 
    * Returns text between current position and $needle, 
    * inclusive, or "" if not found. The current index is moved to a point 
    * after the location of $needle, or not moved at all 
    * if nothing is found. 
    */ 
    function skipToStringInTag ($needle) { 
        $pos = strpos ($this->iHtmlText, $needle, $this->iHtmlTextIndex); 
        if ($pos === false) { 
            return ""; 
        } 
        $top = $pos + strlen($needle); 
        $retvalue = substr ($this->iHtmlText, $this->iHtmlTextIndex, $top - $this->iHtmlTextIndex); 
        $this->setTextIndex ($top); 
        return $retvalue; 
    } 


function HtmlParser_ForFile ($fileName) { 
    return HtmlParser_ForURL("$fileName); 


function HtmlParser_ForURL ($url) { 
    $fp = fopen ($url, "r"); 
    $content = ""; 
    while (true) { 
        $data = fread ($fp, 8192); 
        if (strlen($data) == 0) { 
            break; 
        } 
        $content .= $data; 
    } 
    fclose ($fp); 
    return new HtmlParser ($content); 


php?> 

로그인 후 댓글쓰기가 가능합니다.

?

List of Articles
번호 분류 제목 날짜 조회 수
337 AutoHotKey ahk) 오토핫키 콤보박스 제어하기 file 2013.10.30 38180
336 WindowsTip 탐색기로 ftp 폴더 바로열기 file 2013.12.03 19665
335 WindowsTip Windows-XP 의 [Prefetch] 폴더에 대하여[C:\WINDOWS\Prefetch] 2013.12.04 24630
334 WindowsTip 스마트폰으로 오실로스코프 사용하기(App:OsciPrime Oscilloscope Legacy) file 2013.12.27 23302
333 WindowsTip 인터넷 익스플로러 속도개선 프로그램 file 2014.03.26 4764
332 AutoHotKey 엑셀 셀 복사하기(복사제한된 엑셀등) 1 file 2014.04.01 7781
331 AutoHotKey ahk) autohotkey controlgettext 이름을 마우스커서에 졸졸 따라다니게 하기 file 2014.04.01 12182
330 컴퓨터잡담 동영상 자르기 프로그램 file 2014.04.14 2704
329 컴퓨터잡담 구글 문서도구 스프레드시트로 바코드 입력하기 2014.07.19 4667
328 Excel 오피스 2003, 2007, 2010... 삭제할 수 없을 때 제거 방법 2014.07.31 6881
327 Excel 배열수식 다중조건의 일치하는 값 불러 오기 2014.08.20 14176
326 Server XE DB 튜닝 2014.09.13 4377
325 Excel Google 스프레드시트 함수 2014.10.04 7168
324 Excel 엑셀 여러가지 기능 2014.11.10 5010
323 [Docs]스프레드시트 Google Spreadsheet (Docs) 에서 우리은행 환율정보 이용하기 2014.11.11 29869
322 [Docs]스프레드시트 음력변환 2014.11.11 17854
321 [Docs]스프레드시트 쇼킹한 웹 긁어오기 2014.11.11 3903
320 HTMLPHPMSQL PHP강좌 MySQL 연동 2014.11.11 5801
319 Excel 엑셀에서 날짜합계 구하는 함수(Sumproduct 함수 이해하기) 2014.12.13 9908
318 WindowsTip 익스플로러 ftp 정상화 시키기 file 2014.12.23 1660
Board Pagination Prev 1 ... 28 29 30 31 32 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소