ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
HTMLParser.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleExcel\Parser;
4 
6 
13 class 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 }