Author: Krzysztof Kubacki
Date: 04-03-2019
Converts HTML table to other format
This project is in ALPHA, meaning it is not fully functional!
Inspired by project https://github.com/tremblay/HTML-Table-to-JSON
Given we have table bellow
First name | Last name | Points |
---|---|---|
Jill | Smith | 50 |
Eve | Jackson | 94 |
When we run the code bellow
<?php
$converter = HtmlTableConverter\HtmlTableConverterFactory::fromHtml($tableHtml);
var_dump($converter->convert());
Then will get the following result
array(3) {
[0] =>
array(3) {
[0] =>
string(10) "First name"
[1] =>
string(9) "Last name"
[2] =>
string(6) "Points"
}
[1] =>
array(3) {
'First name' =>
string(4) "Jill"
'Last name' =>
string(5) "Smith"
'Points' =>
string(9) "<b>50</b>"
}
[2] =>
array(3) {
'First name' =>
string(3) "Eve"
'Last name' =>
string(7) "Jackson"
'Points' =>
string(9) "<b>94</b>"
}
}
<?php
$converter = HtmlTableConverter\HtmlTableConverterFactory::fromHtml($tableHtml);
$converter->stripTagsFromColumnValues();
var_dump($converter->convert());
array(3) {
[0] =>
array(3) {
[0] =>
string(10) "First name"
[1] =>
string(9) "Last name"
[2] =>
string(6) "Points"
}
[1] =>
array(3) {
'First name' =>
string(4) "Jill"
'Last name' =>
string(5) "Smith"
'Points' =>
- string(9) "<b>50</b>"
+ string(2) "50"
}
[2] =>
array(3) {
'First name' =>
string(3) "Eve"
'Last name' =>
string(7) "Jackson"
'Points' =>
- string(9) "<b>94</b>"
+ string(2) "94"
}
}
<?php
$converter = HtmlTableConverter\HtmlTableConverterFactory::fromHtml($tableHtml);
$converter->doNotIncludeHeaderRowInResult();
var_dump($converter->convert());
array(2) {
- [0] =>
- array(3) {
- [0] =>
- string(10) "First name"
- [1] =>
- string(9) "Last name"
- [2] =>
- string(6) "Points"
- }
[0] =>
array(3) {
'First name' =>
string(4) "Jill"
'Last name' =>
string(5) "Smith"
'Points' =>
string(9) "<b>50</b>"
}
[1] =>
array(3) {
'First name' =>
string(3) "Eve"
'Last name' =>
string(7) "Jackson"
'Points' =>
string(9) "<b>94</b>"
}
}
<?php
$converter = HtmlTableConverter\HtmlTableConverterFactory::fromHtml($tableHtml);
$converter->convert(\HtmlTableConverter\Converter\Type::JSON);
- array
- json
In command line
composer require bystro/html-table-converter
or composer.json
"require": {
"bystro/html-table-converter": "^1.0"
}