Repository navigation
Expand file tree
/
Copy pathHTMLParser.php
More file actions
79 lines (74 loc) · 2.81 KB
/
Copy pathHTMLParser.php
File metadata and controls
79 lines (74 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace Faisalman\SimpleExcel\Parser;
use Faisalman\SimpleExcel\Enums\SimpleExcelException;
use Faisalman\SimpleExcel\Spreadsheet\Workbook;
use Faisalman\SimpleExcel\Spreadsheet\Worksheet;
/**
* SimpleExcel class for parsing HTML table
*
* @author Faisal Salman
* @package Faisalman\SimpleExcel
*/
class HTMLParser extends BaseParser implements IParser
{
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = 'html';
/**
* Process the loaded file/string
*
* @param DOMDocument $html DOMDocument object of HTML
*/
protected function parseDOM($html){
$this->workbook = new Workbook();
$tables = $html->getElementsByTagName('table');
foreach ($tables as $table) {
$sheet = new Worksheet();
$table_child = $table->childNodes;
foreach ($table_child as $twrap) {
if($twrap->nodeType === XML_ELEMENT_NODE) {
if ($twrap->nodeName === "thead" || $twrap->nodeName === "tbody") {
$twrap_child = $twrap->childNodes;
foreach ($twrap_child as $tr) {
if($tr->nodeType === XML_ELEMENT_NODE && $tr->nodeName === "tr") {
$row = array();
$tr_child = $tr->childNodes;
foreach ($tr_child as $td) {
if ($td->nodeType === XML_ELEMENT_NODE && ($td->nodeName === "th" || $td->nodeName === "td")) {
array_push($row, $td->nodeValue);
}
}
$sheet->insertRecord($row);
}
}
} else if ($twrap->nodeName === "tr") {
$row = array();
$twrap_child = $twrap->childNodes;
foreach ($twrap_child as $td) {
if ($td->nodeType === XML_ELEMENT_NODE && ($td->nodeName === "th" || $td->nodeName === "td")) {
array_push($row, $td->nodeValue);
}
}
$sheet->insertRecord($row);
}
}
}
$this->workbook->insertWorksheet($sheet);
}
}
/**
* Load the string to be parsed
*
* @param string $str String with HTML format
* @param array $options Options
*/
public function loadString($str, $options = NULL){
$html = new \DOMDocument();
$html->loadHTML($str);
$this->parseDOM($html);
}
}