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
번호 분류 제목 날짜 조회 수
437 AutoHotKey autohotkey) 화면보호기, 바탕화면 control 창 열기 3 2012.03.17 8342
436 HTMLPHPMSQL PHP5.4.4 form 변수 전달받기(get, post방식) 2015.07.22 8329
435 Server php) 변수명을 변수값으로 지정방법 10 2013.03.23 8279
434 컴퓨터잡담 자가발전기 원리 1 2012.07.11 8275
433 Server XE Xpress Engine 사이트맵에서 메뉴추가시 저장 안되는 현상 2012.05.12 8255
432 컴퓨터잡담 SendMessage the values are for wparam 1 2009.12.23 8203
431 WindowsTip Iexplorer_익스플로러 오류)0x7c0c5a6c에 있는 명령이 0x7c0c5a6c의 메모리를 참조했습니다. 1 file 2013.02.05 8203
430 AutoHotKey Autohotkey 브라우저 제어 테스트 file 2015.01.26 8203
429 컴퓨터잡담 하드 공유폴더 해제하기 1 2012.03.19 8182
428 WindowsTip MS-DOS 모드에서 Windows 복원 방법 3 2013.02.26 8181
427 컴퓨터잡담 Ghost용 배치 파일 작성 예 1 2009.11.24 8162
426 컴퓨터잡담 Wireshark 이용하여 tcp/ip 패킷내용 추출하기 2 6 2012.10.18 8147
425 WindowsTip imm32.dll 다운 바이러스 오류 해결법(한글이 잘 안써지는 문제 등) 1 file 2013.01.23 8140
424 컴퓨터잡담 windows xp 정품인증 크랙 2011.08.01 8118
423 WindowsTip 스윙 브라우저와 크롬 브라우저 비교 file 2013.02.14 8117
422 컴퓨터잡담 tasklist /SVC svchost 시스템 서비스 프로세스 보기 2010.01.21 8117
421 WindowsTip ISO 파일을 USB에 굽는 방법 [4GB 넘는 ISO 파일 USB에 굽는 방법] 2019.04.13 8113
420 WindowsTip windows Arp Spoofing Tool 5 2013.01.05 8079
419 컴퓨터잡담 [autohotkey] 시스템 레지스트리 수정, 삭제 1 3 2010.08.14 8062
418 AutoHotKey autohotkey) 스크린세이버 활성화 / 비활성화 시키기. 2012.03.17 8035
Board Pagination Prev 1 ... 23 24 25 26 27 ... 46 Next
/ 46

http://urin79.com

우린친구블로그

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


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

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

설치 취소