PHP code example of dcarbone / table-mapper

1. Go to this page and download the library: Download dcarbone/table-mapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dcarbone / table-mapper example snippets


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

<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>
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();