Parse HTML tags and implement Quick Find nodes to get node information

Source: Internet
Author: User
Tags closing tag
Detailed introduction and use, please click Source sources.
  1. /**
  2. * HTML Tag Parsing package
  3. *
  4. * @category Tagparse
  5. * @package Tagparse
  6. * @author Kun
  7. * @copyright Kun
  8. * @license http://www.php.net/license/3_01.txt PHP license 3.01
  9. * @version 1.0
  10. * @link http://www.blogkun.com
  11. * @since 1.0
  12. */
  13. namespace Tagparse;
  14. /**
  15. * Tagdomroot
  16. *
  17. * @category Tagparse
  18. * @package Tagparse
  19. * @author Kun
  20. * @copyright Kun
  21. * @license http://www.php.net/license/3_01.txt PHP license 3.01
  22. * @version 1.0
  23. * @link http://www.blogkun.com
  24. * @since 1.0
  25. */
  26. Class Tagdomroot
  27. {
  28. Public $tag = ' root ';
  29. Public $plaintext;
  30. Public $child = Array ();
  31. Public $level = 0;
  32. public static $TagParseError = false;
  33. protected static $TagSet = Array ();
  34. protected static $FoundNode = Array ();
  35. public static $ErrorTag = Array ();
  36. /**
  37. * Initproperty
  38. *
  39. * @access Public
  40. *
  41. * @return NULL
  42. */
  43. Public Function Initproperty ()
  44. {
  45. $TagParseError = false;
  46. $TagSet = Array ();
  47. $FoundNode = Array ();
  48. $DumpScriptCode = Array ();
  49. $ErrorTag = Array ();
  50. }
  51. /**
  52. * __construct
  53. *
  54. * @param string $STR the tag string to be parse.
  55. *
  56. * @access Public
  57. *
  58. * @return Tagdomroot
  59. */
  60. Public function __construct ($STR)
  61. {
  62. $this->_removenoise ($STR);
  63. if ($str = = = null) {
  64. Self:: $TagParseError = true;
  65. } else {
  66. $l = Strpos ($str, ' < ');
  67. if ($l!== false) {
  68. $this->plaintext = substr ($str, 0, $l);
  69. }
  70. $res = Preg_match_all (' ~> (. *?) <~s ', $str, $matches);
  71. if ($res!== false && $res > 0) {
  72. $this->plaintext. = Implode ($matches [1]);
  73. }
  74. $r = Strrpos ($str, ' > ');
  75. if ($r!== false) {
  76. $this->plaintext. = substr ($str, $r + 1);
  77. }
  78. $tagCollect = Array ();
  79. $attrCollect = Array ();
  80. $innerContentCollect = Array ();
  81. if ($this->parsetag ($str, $tagCollect, $attrCollect, $innerContentCollect) = = = = False) {
  82. Self:: $TagParseError = true;
  83. }
  84. foreach ($tagCollect as $index = = $tag) {
  85. $this->child[] = new Tagdomnode ($tag, $this, $attrCollect [$index], $innerContentCollect [$index], $this->level+1 );
  86. }
  87. }
  88. }
  89. /**
  90. * Parsetag
  91. *
  92. * @param mixed $str Description.
  93. * @param mixed & $tagCollect Description.
  94. * @param mixed & $attrCollect Description.
  95. * @param mixed & $innerContentCollect Description.
  96. *
  97. * @access protected
  98. *
  99. * @return Boolean Value.
  100. */
  101. protected function Parsetag ($STR, array & $tagCollect, array & $attrCollect, array & $innerContentCollect)
  102. {
  103. $selfClosingTags = Array (' img ' = = 1, ' br ' = + 1, ' input ' = 1, ' meta ' = 1, ' link ' = 1, ' hr ' = 1, ' base ' = 1, ' embed ' = 1, ' spacer ' and 1 ';
  104. $end =-2;
  105. $close = 0;
  106. $error = false;
  107. $tag = ";
  108. while (true) {
  109. $l = Strpos ($str, ' < ', $end +strlen ($tag) +2);
  110. if ($l = = = False) {//parse End
  111. Break
  112. }
  113. if (Strpos (substr ($str, $l, 2), '/')!== false) {//surplus closing Tag,discard
  114. $error = true;
  115. $end = $l +strlen ($tag);
  116. Self:: $ErrorTag [] = substr ($str, $l, Strpos ($str, ' > ', $l)-$l + 1);
  117. Continue
  118. }
  119. $r = Strpos ($str, ' > ', $l);
  120. $tag = substr ($str, $l +1, $r-$l-1);
  121. if (!ctype_alpha ($tag [0]) | | strpos ($tag, ' < ')!== false) {
  122. $end = $r + 1;
  123. Continue
  124. }
  125. $tag = Preg_replace ("~\n+~", "', $tag);
  126. $space = Strpos ($tag, ');
  127. if ($space!== false) {
  128. $attrCollect [] = substr ($tag, $space + 1);
  129. $tag = substr ($tag, 0, $space);
  130. } else {
  131. $attrCollect [] = ";
  132. }
  133. $tagCollect [] = $tag;
  134. if (Isset ($selfClosingTags [$tag])) {
  135. $innerContentCollect [] = ";
  136. $end = $r-strlen ($tag)-2;
  137. $close = $r +1;
  138. Continue
  139. }
  140. $countOpen =-1;
  141. $open = Strpos ($str, ' < '. $tag, $close);
  142. $close = Strpos ($str, " , $open);
  143. if ($close = = = False) {//surplus opening tag
  144. $innerContentCollect [] = substr ($str, $r + 1);
  145. $error = true;
  146. Self:: $ErrorTag [] = ' < '. $tag. ' > ';
  147. Break
  148. }
  149. $start = $open;
  150. while ($open < $close && $open!== false) {
  151. $countOpen + +;
  152. $open = Strpos ($str, ' < '. $tag, $open +strlen ($tag));
  153. }
  154. while ($countOpen > 0 && $close!== false) {
  155. $open = Strpos ($str, ' < '. $tag, $close +strlen ($tag) +3);
  156. $close = Strpos ($str, " , $close +strlen ($tag) +3);
  157. if ($close = = = False) {
  158. Break
  159. }
  160. $countOpen--;
  161. while ($open < $close && $open!== false) {
  162. $open = Strpos ($str, ' < '. $tag, $open +strlen ($tag) +3);
  163. $countOpen + +;
  164. }
  165. }
  166. if ($close = = = False) {//label closed not paired
  167. $innerContentCollect [] = substr ($str, $r + 1);
  168. $error = true;
  169. Break
  170. }
  171. $end = $close;
  172. $r = Strpos ($str, ' > ', $start);
  173. $innerContentCollect [] = substr ($str, $r +1, $end-$r-1);
  174. }
  175. return! $error;
  176. }
  177. /**
  178. * _removenoise
  179. *
  180. * @param string & $STR The tag string to is parse.
  181. *
  182. * @access Private
  183. *
  184. * @return String
  185. */
  186. Private Function _removenoise (& $STR)
  187. {
  188. $str = Preg_replace (' ~ ~is ', ' ', $str);
  189. $str = Preg_replace (' ~ ~is ', ' ', $str);
  190. $str = preg_replace (' ~~is ', ' ', $str);
  191. }
  192. /**
  193. * Parseselectors
  194. *
  195. * @param string $selectors user s Select condition.
  196. * @param array & $selectorsTag tags
  197. * @param array & $selectorsAttr attributes
  198. *
  199. * @access protected
  200. *
  201. * @return NULL
  202. */
  203. protected function Parseselectors ($selectors, array & $selectorsTag, array & $selectorsAttr)
  204. {
  205. Preg_match_all (' ~ ([\w\d]+) (\[[\w\d-= ". _/]+\])? ~ ', $selectors, $matches);
  206. $selectorsTag = $matches [1];
  207. foreach ($matches [2] as $key = + $value) {
  208. $selectorsAttr [$key] = array ();
  209. if ($value!== ") {
  210. Preg_match_all (' ~ ([\w\d-]+] = "([\w\d-. _/]+)" ~ ', $value, $matches);
  211. foreach ($matches [1] as $index = + $attr) {
  212. $selectorsAttr [$key] [$attr] = $matches [2][$index];
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * Find
  219. *
  220. * @param mixed $selectors user ' s select condition.
  221. * @param array $selectorsTag tags.
  222. * @param array $selectorsAttr attributes.
  223. *
  224. * @access Public
  225. *
  226. * @return Array
  227. */
  228. Public function Find ($selectors, $selectorsTag = Array (), $selectorsAttr = Array ())
  229. {
  230. if ($selectors!== null) {
  231. $this->parseselectors ($selectors, $selectorsTag, $selectorsAttr);
  232. }
  233. Var_dump ($selectorsTag, $selectorsAttr); exit ();
  234. if (!empty ($selectorsTag)) {
  235. $this->seek ($selectorsTag, $selectorsAttr);
  236. foreach ($this->child as $key = + $node) {
  237. $node->find (null, $SELECTORSTAG, $selectorsAttr);
  238. }
  239. }
  240. if ($selectors!== null) {
  241. $res = self:: $FoundNode;
  242. Self:: $FoundNode = Array ();
  243. return $res;
  244. }
  245. }
  246. /**
  247. * Findglobal
  248. *
  249. * @param string $selectors user s Select condition.
  250. *
  251. * @access Public
  252. *
  253. * @return Array
  254. */
  255. Public Function Findglobal ($selectors)
  256. {
  257. $space = Strpos ($selectors, ' ', Strpos ($selectors, '] '));
  258. if ($space = = = False) {
  259. return $this->findoneglobal ($selectors);
  260. } else {
  261. $selectorsAttr = Array ();
  262. $selectorsTag = Array ();
  263. $this->findoneglobal (substr ($selectors, 0, $space), false);
  264. $this->parseselectors (substr ($selectors, $space + 1), $selectorsTag, $selectorsAttr);
  265. if (!empty (self:: $FoundNode) &&!empty ($selectorsTag)) {
  266. $nodes = self:: $FoundNode;
  267. Self:: $FoundNode = Array ();
  268. foreach ($nodes as $key = = $node) {
  269. $node->seek ($selectorsTag, $selectorsAttr);
  270. }
  271. }
  272. }
  273. $res = self:: $FoundNode;
  274. Self:: $FoundNode = Array ();
  275. return $res;
  276. }
  277. /**
  278. * Seek
  279. *
  280. * @param array $selectorsTag tags.
  281. * @param array $selectorsAttr attributes.
  282. *
  283. * @access protected
  284. *
  285. * @return NULL
  286. */
  287. protected function Seek ($selectorsTag, $selectorsAttr)
  288. {
  289. foreach ($this->child as $key = + $node) {
  290. $isFind = true;
  291. if ($node->tag = = = = $selectorsTag [0]) {
  292. foreach ($selectorsAttr [0] as $attrName = + $value) {
  293. if (Isset ($node->attr[$attrName])
  294. && (Preg_match (' ~.*? '. $value. *?~ ', $node->attr[$attrName]) > 0
  295. || Preg_match (' ~^ '. $value. ' $~ ', $node->attr[$attrName]) > 0
  296. || Preg_match (' ~^ '. $value. ' ~ ', $node->attr[$attrName]) > 0
  297. || Preg_match (' ~ '. $value. ' $~ ', $node->attr[$attrName]) > 0)
  298. ) {
  299. Continue
  300. } else {
  301. $isFind = false;
  302. Break
  303. }
  304. }
  305. } else {
  306. $isFind = false;
  307. }
  308. if ($isFind) {
  309. if (count ($selectorsTag) = = = 1) {
  310. Self:: $FoundNode [] = $node;
  311. } else {
  312. $node->seek (
  313. Array_slice ($selectorsTag, 1),
  314. Array_slice ($selectorsAttr, 1)
  315. );
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * Findoneglobal
  322. *
  323. * @param string $selector user s Select condition.
  324. * @param bool $isReturn weather return value.
  325. *
  326. * @access Public
  327. *
  328. * @return Array
  329. */
  330. Public Function Findoneglobal ($selector, $isReturn = True)
  331. {
  332. Preg_match (' ~ ([\w\d]+) (\[[\w\d-= ". _/]+\])? ~ ', $selector, $matches);
  333. $tag = $matches [1];
  334. $attr = Array ();
  335. if (Isset ($matches [2])) {
  336. Preg_match_all (' ~ ([\w\d-]+] = "([\w\d-. _/]+)" ~ ', $matches [2], $matches);
  337. foreach ($matches [1] as $key = + $value) {
  338. $attr [$value] = $matches [2][$key];
  339. }
  340. }
  341. if (Isset (self:: $TagSet [$tag])) {
  342. foreach (self:: $TagSet [$tag] as $attrValue = = $nodeArray) {
  343. $isFind = true;
  344. foreach ($attr as $attrName = = $value) {
  345. if (Preg_match (' ~ '. $attrName. ' =".*? '. $value. '. *? ' ~ ', $attrValue)
  346. || Preg_match (' ~ '. $attrName. ' = "'. $value. '. *? ' ~ ', $attrValue)
  347. || Preg_match (' ~ '. $attrName. ' =".*? '. $value. ' " ~ ', $attrValue)
  348. || Preg_match (' ~ '. $attrName. ' = "'. $value. '" ~ ', $attrValue)
  349. ) {
  350. Continue
  351. } else {
  352. $isFind = false;
  353. Break
  354. }
  355. }
  356. if ($isFind) {
  357. foreach ($nodeArray as $key = = $node) {
  358. Self:: $FoundNode [] = $node;
  359. }
  360. }
  361. }
  362. }
  363. if ($isReturn) {
  364. $res = self:: $FoundNode;
  365. Self:: $FoundNode = Array ();
  366. return $res;
  367. }
  368. }
  369. }
  370. /**
  371. * Tagdomnode
  372. *
  373. * @uses Tagdomroot
  374. *
  375. * @category Tagparse
  376. * @package Tagparse
  377. * @author Kun
  378. * @copyright Kun
  379. * @license http://www.php.net/license/3_01.txt PHP license 3.01
  380. * @version 1.0
  381. * @link http://www.blogkun.com
  382. * @since 1.0
  383. */
  384. Class Tagdomnode extends Tagdomroot
  385. {
  386. Public $attr = Array ();
  387. public $parent = null;
  388. /**
  389. * __construct
  390. *
  391. * @param mixed $tag tag.
  392. * @param mixed $parent parent node.
  393. * @param mixed $attr attribute.
  394. * @param mixed $innerContent tag content.
  395. * @param mixed $level node level.
  396. *
  397. * @access Public
  398. *
  399. * @return Tagdomnode
  400. */
  401. Public function __construct ($tag, $parent, $attr, $innerContent, $level)
  402. {
  403. $this->tag = $tag;
  404. $this->parent = $parent;
  405. $this->_parseattr ($attr);
  406. $this->level = $level;
  407. $l = Strpos ($innerContent, ' < ');
  408. if ($l!== false) {
  409. $this->plaintext = substr ($innerContent, 0, $l);
  410. }
  411. $res = Preg_match_all (' ~> (. *?) <~s ', $innerContent, $matches);
  412. if ($res!== false && $res > 0) {
  413. $this->plaintext. = Implode ($matches [1]);
  414. } else {
  415. $this->plaintext. = $innerContent;
  416. }
  417. $r = Strrpos ($innerContent, ' > ');
  418. if ($r!== false) {
  419. $this->plaintext. = substr ($innerContent, $r + 1);
  420. }
  421. $tagCollect = Array ();
  422. $attrCollect = Array ();
  423. $innerContentCollect = Array ();
  424. if ($this->parsetag ($innerContent, $tagCollect, $attrCollect, $innerContentCollect) = = = = False) {
  425. Self:: $TagParseError = true;
  426. }
  427. foreach ($tagCollect as $index = = $tag) {
  428. $this->child[] = new Tagdomnode ($tag, $this, $attrCollect [$index], $innerContentCollect [$index], $this->level+1 );
  429. }
  430. if (!isset (self:: $TagSet [$this->tag])) {
  431. Self:: $TagSet [$this->tag] = array ();
  432. }
  433. if (!isset (self:: $TagSet [$this->tag][$attr])) {
  434. Self:: $TagSet [$this->tag][$attr] = array ();
  435. }
  436. Self:: $TagSet [$this->tag][$attr] = & $this;
  437. }
  438. /**
  439. * _parseattr
  440. *
  441. * @param string $str attribute string.
  442. *
  443. * @access Public
  444. *
  445. * @return NULL
  446. */
  447. Private Function _parseattr ($STR)
  448. {
  449. Preg_match_all (' ~ (? [ \w-]+) = "(?. *?)" ~s ', $str, $matches);
  450. foreach ($matches [' attrname '] as $key = + $value) {
  451. $this->attr[$value] = $matches [' AttrValue '] [$key];
  452. }
  453. }
  454. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.