ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
HTMLParser.php
Go to the documentation of this file.
1<?php
2
3namespace SimpleExcel\Parser;
4
6
13class HTMLParser extends BaseParser implements IParser
14{
21 protected $file_extension = 'html';
22
28 private function parseDOM($html){
29 $tables = $html->getElementsByTagName('table');
30 $field = array();
31 foreach ($tables as $table) {
32 $table_child = $table->childNodes;
33 foreach ($table_child as $twrap) {
34 if($twrap->nodeType === XML_ELEMENT_NODE) {
35 if ($twrap->nodeName === "thead" || $twrap->nodeName === "tbody") {
36 $twrap_child = $twrap->childNodes;
37 foreach ($twrap_child as $tr) {
38 if($tr->nodeType === XML_ELEMENT_NODE && $tr->nodeName === "tr") {
39 $row = array();
40 $tr_child = $tr->childNodes;
41 foreach ($tr_child as $td) {
42 if ($td->nodeType === XML_ELEMENT_NODE && ($td->nodeName === "th" || $td->nodeName === "td")) {
43 array_push($row, $td->nodeValue);
44 }
45 }
46 array_push($field, $row);
47 }
48 }
49 } else if ($twrap->nodeName === "tr") {
50 $row = array();
51 $twrap_child = $twrap->childNodes;
52 foreach ($twrap_child as $td) {
53 if ($td->nodeType === XML_ELEMENT_NODE && ($td->nodeName === "th" || $td->nodeName === "td")) {
54 array_push($row, $td->nodeValue);
55 }
56 }
57 array_push($field, $row);
58 }
59 }
60 }
61 }
62 $this->table_arr = $field;
63 }
64
70 public function loadFile($file_path) {
71
72 if (!$this->isFileReady($file_path)) {
73 return;
74 }
75
76 $html = new \DOMDocument();
77 $html->loadHTMLFile($file_path);
78 $this->parseDOM($html);
79 }
80
86 public function loadString($str){
87 $html = new \DOMDocument();
88 $html->loadHTML($str);
89 $this->parseDOM($html);
90 }
91}
isFileReady($file_path)
Check whether file exists, valid, and readable.
Definition: BaseParser.php:169
parseDOM($html)
Process the loaded file/string.
Definition: HTMLParser.php:28
loadFile($file_path)
Load the HTML file to be parsed.
Definition: HTMLParser.php:70
loadString($str)
Load the string to be parsed.
Definition: HTMLParser.php:86
$html
Definition: example_001.php:87
define parser interface
Definition: IParser.php:14