詳細介紹使用請點選來源碼出處。
-
/**
- * htmlmap 解析器
- *
- * @category TagParse
- * @package TagParse
- * @author 這個
- * @copyright 2014 * @version 1.0
- * @link http://www.blogkun.com
- * @since 1.0
- */
- 命名空間 TagParse;
-
- /**
- * TagDomRoot
- *
- * @category TagParse
- * @package TagParse
- * @author kun
- * @copyright 2014 kun; license http://www.php.net/license/3_01.txt PHP 許可證3.01
- * @版本1.0
- * @link http://www.blogkun.com
- * @since 1.0
- */
- class TagDomRoot
- {
- public $tag = 'root';
- public $plaintext;
- public $child = array();
- public $level = 0;
- public static $ TagParseError = false;
- protected static $TagSet = array();
- protected static $FoundNode = array();
- public static $ErrorTag = array();
-
- 🎝>*/
- public function initProperty()
- {
- $TagParseError = false;
- $TagSet = array();
- $FoundNode = array(); $ErrorTag = array();
- }
-
- /**
- * initProperty
- *
- * @access public
- *
- * @return null
- */
- public function __construct($str)
- {
- $
- $ this->_removeNoise($str);
- if ($str === null) {
- self::$TagParseError = true;
- } else {
- $l = strpos($str ,> } else {
- $l = strpos($str , ' if ($l !== false) {
- $this->plaintext = substr($str, 0, $l);
- }
- $res = preg_match_all( '~>(.*?) if ($res !== false && $res >; 0) {
- $this->plaintext .= 內爆($matches[1]);
- }
- $r = strrpos($str, '>');
- if ($ r !== false) {
- $this->plaintext 。 🎜 >
- if ($this->parseTag($str, $tagCollect, $attrCollect, $innerContentCollect) === false) {
- self::$TagParseError = true;
- }
- self::$TagParseError = true;
- }
-
-
- foreach ($tagCollect as $index => $tag) {
- $this->child[] = new TagDomNode($tag , $this, $attrCollect[$index], $innerContentCollect[$index], $ this ->level 1);
- }
- }
- }
-
- /**
- * __construct
- *
- * @param string $str 要解析的標籤字串。
- *
- * @access public
- *
- * @return TagDomRoot
- */
- 受保護的函數parseTag($str, array &$taglect, array &$attrCollect, array &$innerContentCollect)
- {
- $selfClosingTags = array('img' => 1, 'br ' => 1, '輸入' => 1, '元' => 1、 '連結'=> 1、'小時'=> 1、'基礎'=> 1、'嵌入'=> 1、'間隔'=> 1);
- $end = -2;
- $close = 0;
- $error = false;
- $tag = '';
- while (true) {
- $l = strpos($str, ' if ($l === false) {//解析結束
- break;
- }
- if (strpos(substr($str, $l, 2), '/' ) !== false) {//多餘的結束標記,丟棄
- $error = true;
- $end = $l strlen( $tag);
- self::$ErrorTag[] = substr( $ str, $l, strpos($str, '>', $l)-$l 1);
- 繼續;
- }
-
- $r = strpos($str, '>' , $l);
- $tag = substr($str, $l 1, $r-$l-1);
- if (!ctype_alpha($tag[0]) || strpos($tag, ' $end = $r 1;
- 繼續;
- }
- $tag = preg_replace("~n ~", ' ', $tag);
- $space = strpos($tag, ' ');
- if ($space !== false) {
- $attrCollect[] = substr($tag, $space 1);
- $tag = substr($tag, 0, $space);
- } else {
- $attrCollect[] = '' ;
- }
- $tagCollect[] = $tag;
- if (isset($selfClosingTags[$tag])) {
- $innerContentCollect[] = '';
- $end = $> $innerContentCollect[] = '';
- $end = $ r-strlen($tag)-2;
- $close = $r 1;
- 繼續;
- }
-
- $countOpen = -1;
- $open = strpos($ str, ' $close = strpos($str, ''.$tag.'>', $open);
- if ($close === false) {//告白的開始標記
- $innerContentCollect[] = substr($str, $r 1);
- $error = true;
- self::$ErrorTag[] = '';
- 規則;
- }
- $start = $open;
- while ($open
- $close && $open !== false) {
- $countOpen ;
- $open = strpos($str, ' }
-
- while ($countOpen > 0 && $close ! == false) {
- $open = strpos($str, ' $close = strpos($str, ''.$tag.'>', $close strlen($tag) 3);
- if ($close === false) {
- break ;
- }
- $countOpen--;
- while ($open $open = strpos($str, ' $countOpen ;
- }
- }
- if ($close === false) {//標籤閉合不佈局
- $innerContentCollect[] = substr($str, $r 1);
- $error = true;
- 中斷;
- }
- $end = $close;
- $r = strpos($ str, '> ', $start);
- $innerContentCollect[] = substr($str, $r 1, $end - $r - 1);
- }
-
- return !$錯誤;
- }
-
- /**
- * _removeNoise
- *
- * @param string &$str 要解析的標籤字串。
- *
- * @access private
- *
- * @return string
- */
- 私有函數_removeNoise(&$str)
- {
- $str = preg_replace('~~is', '', $str);
- $str = preg_replace('~~is', ' ', $str);
- $str = preg_replace('~*?>~is', '', $str);
- }
-
- /**
- * parseSelectors
- *
- * @param string $selectors 使用者的選擇條件。
- * @param array &$selectorsTag 標籤
- * @param array &$selectorsAttr 屬性
- *
- * @access protected
- *
- * @return null *
- * @return null
- */
- protected function parseSelectors ( $selectors, 陣列&$selectorsTag, 陣列&$selectorsAttr)
- {
- preg_match_all('~([wd] )([[wd -="._/ ] ])?~', $selectors, $ matches);
- $selectorsTag = $matches[1];
- foreach ($matches[2] as $key =>; $value) {
- $selectorsAttr[$key] = array();
- if ($value !== '') {
- preg_match_all('~([wd-] )="([wd -._/] )"~', $value, $matches);
- foreach ($matches[1] as $index => $attr) {
- $selectorsAttr[$key][$attr] = $matches[2][$index];
- }
- }
- }
- }
-
- /**
- * find
- *
- * @param mix $selectors 使用者的選擇條件。
- * @param array $selectorsTag 標籤。
- * @param array $selectorsAttr 屬性。
- *
- * @access public
- *
- * @return array
- */
- public function find($selectors , $selectorsTag = array(), $selectorsAttr = array())
- {
- if ($selectors !== null) {
- $this->parseSelectors($selectors, $selectorsTag, $selectorsAttr );
- }
- var_dump($selectorsTag, $selectorsAttr);exit();
- if (!empty($selectorsTag)) {
- $this->seek($selectorsTag, $selectorsAttr) );
- foreach ($this->child as $key => $node) {
- $node->find(null, $selectorsTag, $selectorsAttr);
- }
- }
-
- if ($selectors !== null) {
- $res = self::$FoundNode;
- self::$FoundNode = array(); return $res ; } }
-
- /**
- * findGlobal
- *
- * @param string $selectors 使用者的選擇條件。
- *
- * @access public
- *
- * @return array
- */
- public function findGlobal($selectors)
- {
- $space = strpos($selectors, ' ', strpos($selectors, ']' ));
- if ($space === false) {
- return $this->findOneGlobal($selectors);
- } else {
- $selectorsAttr = array();
- $
- $selectorsAttr = array();
- $ selectorsTag = array();
- $this->findOneGlobal(substr($selectors, 0, $space), false);
- $this->parseSelectors(substr($selectors, $space 1) , $selectorsTag , $selectorsAttr);
- if (!empty(self::$FoundNode) && !empty($selectorsTag)) {
- $nodes = self::$FoundNode;
- self::$ FoundNode = 陣列();
- foreach ($nodes as $key => $node) {
- $node->seek($selectorsTag, $selectorsAttr);
- }
- }
-
-
- }
-
- $res = self::$FoundNode;
- self::$FoundNode = array();
-
- return $res;
- }
-
- /**
- * 尋找
- *
- * @param 陣列 $selectorsTag 標籤。
- * @param 陣列 $selectorsAttr 屬性。
- *
- * @access protected
- *
- * @return null
- */
- protected functioneek($selectorsTag, $selectorsAttr)
- {
- foreach ($this->child as $key =>; $node) {
- $isFind = true;
- if ($node->tag === $selectorsTag[0]) {
- foreach ($selectorsAttr[0] as $attrName => $ value) {
-
- if ( isset($node->attr[$attrName])
- && (preg_match('~.*? '.$價值。 .*?~', $node ->attr[$attrName]) > 0
- || preg_match('~^'.$value.'$~', $node->attr[$attrName]) > 0
- | | preg_match('~^'.$value.' ~', $node->attr[$attrName]) > 0
- || preg_match('~ '.$value.'$~', $node ->attr[$attrName]) > 0)
- ) {
- 續;
- } else {
- $isFind = false ;
- 中斷;
- }
- }
- } else {
- $isFind = false;
- }
- if ($isFind) {
- if (count($selectorsTag ) ) === 1) {
- self::$ FoundNode[] = $node;
- } else {
- $node->seek(
- array_slice($selectorsTag, 1),
- array_slice($selectorsAttr, 1)
- );
- }
- }
- }
- }
-
- /***/ $選擇器, $isReturn = true)
- {
- preg_match('~([wd] )([[wd -="._/] ])?~', $selector, $matches);
- $tag = $matches[1];
- $ attr = array();
- if (isset($matches[2])) {
- preg_match_all('~([wd-] )= "( [wd-._/] )"~', $matches[2], $matches);
- foreach ($matches[1] as $key => $value) {
- $attr[$ value] = $matches [2][$key];
- }
- }
- if (isset(self::$TagSet[$tag])) {
- foreach (self::$TagSet [$ tag] as $attrValue => $nodeArray) {
- $isFind = true;
- foreach ($attr as $attrName => $value) {
- if (preg_match('~'.$attrName .' = ".*? '.$value.' .*?"~', $attrValue)
- || preg_match('~'.$attrName.'="'.$value.' .*?" ~' , $attrValue)
- || preg_match('~'.$attrName.'=".*? '.$value.'"~', $attrValue)
- || preg_match('~'.$attrName. '="'.$value.'"~', $attrValue)
- ) {
- 繼續;
- } else {
- $isFind = false;
- 中斷;
- }
- }
- if ($isFind) {
- foreach ($nodeArray as $key =>; $node) {
- self::$FoundNode[] = $key =>; $node) {
- 是 $節點;
- }
- }
- }
- }
- if ($isReturn) {
- $res = self ::$FoundNode;
- self::$FoundNode = array( );
-
- 回傳$res;
- }
- }
- }
-
- /**
- * TagDomNode
- *
- * @uses TagDomRoot
- *
- * @category TagParse
- * @package TagParse
- * @author kun
- * @package TagParse
- * @author kun * @copyright 2014 kun
- * @license http://www.php.net/license/3_01.txt PHP 許可證3.01
- * @version 1.0
- * @link http://www. php.net/license/3_01.txt php.net/license/3_01.txt blogkun.com
- * @since 1.0
- */
- class TagDomNode 擴充 TagDomRoot
- {
- public $attr = array();
- public $parent = null;
-
- /**
- * __construct
- *
- * @param Mixed $tag 標籤。
- * @param Mixed $parent 父節點。
- * @param Mixed $attr 屬性。
- * @param Mixed $innerContent 標記內容。
- * @param 混合 $level 節點層級。
- *
- * @access public
- *
- * @return TagDomNode
- */
- 公用函數__construct($tag, $parent, $attr, $innerContent, $level)
- {
- $this->tag = $tag;
- $this->parent = $parent;
- $this->_parseAttr($attr);
- $this->level = $level;
-
- $l = strpos($ insideContent, ' if ($l !== false) {
- $this->plaintext = substr($innerContent, 0, $l);
- }
- $ res = preg_match_all(' ~ >(.*?) if ($res !== false && $res > 0) {
- $this ->plaintext .= implode($ matches [1]);
- } else {
- $this->plaintext .= $innerContent;
- }
- $r = strrpos($innerContent, '>');
- if ( $ r !== false) {
- $this->plaintext .= substr($innerContent, $r 1);
- }
-
- $tagCollect = array();
- $attrlect = array();
- $innerContentCollect = array();
-
- if ($this->parseTag($innerContent, $tagCollect, $attrCollect) , $innerContentCollect) === false) {
- self::$TagParseError = true;
- }
-
- foreach ($tagCollect as $index =>; $tag) {
- $this->child[] = new TagDomNode($tag, $this, $attrCollect[$index], $innerContentCollect[$index], $this->level 1);
- }
-
- if (!isset(self::$TagSet[$this->tag])) {
- self::$TagSet[$this->tag] = array();
- }
- if (!isset(self::$TagSet[$this->tag][$attr])) {
- self::$TagSet[$this->tag][$attr] = array();
- }
- self::$TagSet[$this->tag][$attr][] = &$this;
- }
-
- /* *
- * _parseAttr
- *
- * @param string $str 屬性字串。
- *
- * @access public
- *
- * @return null
- */
- 解剖函數_parseAttr($str)
- {
- preg_match_all('~(?[w-] )="(?.*?) "~s' , $str, $matches);
- foreach ($matches['attrName'] as $key => $value) {
- $this->attr[$value] = $matches ['attrValue'][ $ key];
- }
- }
- }
複製程式碼
|