2017 © Pedro Peláez
 

library table-mapper

Complex HTML parsing helper

image

dcarbone/table-mapper

Complex HTML parsing helper

  • Tuesday, April 29, 2014
  • by dcarbone
  • Repository
  • 1 Watchers
  • 2 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

table-mapper

TableMapper is a class that I wrote to ease the consumption of complex HTML tables., (*1)

Example:, (*2)

<table>
    <tbody>
        <tr>
            <td colspan="3">Row 0 : Cell 0</td>
            <td rowspan="2">Row 0 : Cell 1</td>
        </tr>
        <tr>
            <td>Row 1 : Cell 0</td>
            <td>Row 1 : Cell 1</td>
            <td>Row 1 : Cell 2</td>
        </tr>
    </tbody>
</table>

Looks something like this:, (*3)

Row 0 : Cell 0 Row 0 : Cell 1
Row 1 : Cell 0 Row 1 : Cell 1 Row 1 : Cell 2

That can be tricky to consume, however. So this class creates a clone of the table element you pass in and creates an in-memory version that looks like this:, (*4)

<table>
    <tbody>
        <tr>
            <td>Row 0 : Cell 0</td>
            <td>Row 0 : Cell 0</td>
            <td>Row 0 : Cell 0</td>
            <td>Row 0 : Cell 1</td>
        </tr>
        <tr>
            <td>Row 1 : Cell 0</td>
            <td>Row 1 : Cell 1</td>
            <td>Row 1 : Cell 2</td>
            <td>Row 0 : Cell 1</td>
        </tr>
    </tbody>
</table>

Which looks like this:, (*5)

Row 0 : Cell 0 Row 0 : Cell 0 Row 0 : Cell 0 Row 0 : Cell 1
Row 1 : Cell 0 Row 1 : Cell 1 Row 1 : Cell 2 Row 0 : Cell 1

Usage, (*6)

$tableHTML = <<<HTML
<h1>Original Table</h1>



Row 0 : Cell 0 Row 0 : Cell 1
Row 1 : Cell 0 Row 1 : Cell 1 Row 1 : Cell 2
HTML; $dom = new \DOMDocument; $dom->loadHTML($tableHTML); $tableMapper = new \DCarbone\TableMapper($dom->getElementsByTagName('table')->item(0)); $tableMapper->createMap(); $dom->appendChild($dom->createElement('h1', 'Parsed Table')); $newTable = $dom->createElement('table'); $dom->appendChild($newTable); foreach($tableMapper->getRowCellMap() as $groupi=>$groupDef) { foreach($groupDef as $rowi=>$cellMap) { $newTr = $dom->createElement('tr'); $newTable->appendChild($newTr); foreach($cellMap as $cellNum) { $cell = $tableMapper->getCell($groupi, $rowi, $cellNum); $newTr->appendChild($dom->createElement('td', $cell->nodeValue)); } } } echo $dom->saveHTML();

This is a very simple class, overall. A few things to note:, (*7)

  • TableMapper does NOT mutate the DOMElement you pass in, it clones and imports it into a new DOM before doing anything.
  • TableMapper does not fix invalid HTML
  • TableMapper does not do your laundry.

The Versions

29/04 2014

dev-master

9999999-dev https://github.com/dcarbone/table-mapper

Complex HTML parsing helper

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • lib-libxml *

 

by Daniel Carbone

html table domdocument

29/04 2014

1.1.1

1.1.1.0 https://github.com/dcarbone/table-mapper

Complex HTML parsing helper

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • lib-libxml *

 

by Daniel Carbone

html table domdocument

21/04 2014

1.1.0

1.1.0.0 https://github.com/dcarbone/table-mapper

Complex HTML parsing helper

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • lib-libxml *

 

by Daniel Carbone

html table domdocument

11/04 2014

1.0.0

1.0.0.0 https://github.com/dcarbone/table-mapper

Complex HTML parsing helper

  Sources   Download

MIT

The Requires

  • php >=5.3.3
  • lib-libxml *

 

by Daniel Carbone

html table domdocument