PHP code example of csskevin / tabledude

1. Go to this page and download the library: Download csskevin/tabledude 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/ */

    

csskevin / tabledude example snippets



// If you are working with composer


// If you installed this software manual



// Fetching HTML content
$content = file_get_contents("https://github.com/csskevin/TableDude/");

$simpleparser = new \TableDude\Parser\SimpleParser($content);
// Parses HTML Tables to PHP Array
$tables = $simpleparser->parseHTMLTables();

foreach($tables as $table)
{
    // Creating an instance of horizontal table
    $horizontalTable = new \TableDude\Converter\HorizontalTable($table);
    // Setting Header Row Index
    $horizontalTable->setHeaderRowIndex(0);
    $groupedTable = $horizontalTable->getGroupedTable();
    foreach($groupedTable as $row)
    {
        echo "Type: " . $row["Type"] . "\n";
        echo "Name: " . $row["Name"] . "\n";
        echo "Commit Message: " . $row["Latest commit message"] . "\n";
        echo "Commit Time: " . $row["Commit time"] . "\n";
        echo "----------------\n";
    }
}



// Fetching HTML content
$content = file_get_contents("https://www.w3schools.com/html/html_tables.asp");

$simpleparser = new \TableDude\Parser\SimpleParser($content);
$tables = $simpleparser->parseHTMLTables();

foreach($tables as $table)
{
    // Creating an instance of horizontal table
    $horizontalTable = new \TableDude\Converter\HorizontalTable($table);
    // Setting Header Row Index
    $horizontalTable->setHeaderRowIndex(0);
    $groupedTable = $horizontalTable->getGroupedTable();
    $fingerprint = $horizontalTable->getFingerprint();
    // Identifying via fingerprint 
    if($fingerprint === 3377504250)
    {
        print_r($groupedTable);
    }
}

$htmlContent = "<html><body><table><tr><td>Cell<td></tr></table></body></html>";
// Initializes an SimpleParser instance with a HTML content.
// __constructor($htmlContent);
$simpleParser = new \TableDude\Parser\SimpleParser($htmlContent);

// Returns an array of parsed html tables
$parsedHTMLTables = $simpleParser->parseHTMLTables();

/*
$parsedHTMLTables would be:

array(
    // First Table
    array(
        // First row
        array(
            "Cell"
        )
    )
)
*/


$parsedHTMLTable = array(
    array(
        "Cell1 in Row1",
        "Cell2 in Row1",
        "Cell3 in Row1"
    ),
    array(
        "Cell1 in Row2",
        "Cell2 in Row2",
        "Cell3 in Row2"
    ),
    array(
        "Cell1 in Row3",
        "Cell2 in Row3",
        "Cell3 in Row3"
    )
);

// __constructor($parsedHTMLTable)
$horizontalTable = \TableDude\Converter\HorizontalTable($parsedHTMLTable);

// Sets the first array in parsedHTMLTable as header row
/*
You can also set the index to -1, then the last row will be selected
*/
$horizontalTable->setHeaderRowIndex(0);
/*
Header would now be
array(
    "Cell1 in Row1",
    "Cell2 in Row1",
    "Cell3 in Row1"
)
*/

// convertes the array
$groupedTable = $horizontalTable->getGroupedTable();

/*
This would return

array(
    array(
        "Cell1 in Row1" => "Cell1 in Row2",
        "Cell2 in Row1" => "Cell2 in Row2",
        "Cell3 in Row1" => "Cell3 in Row2"
    ),
        array(
        "Cell1 in Row1" => "Cell1 in Row3",
        "Cell2 in Row1" => "Cell2 in Row3",
        "Cell3 in Row1" => "Cell3 in Row3"
    ),
)

*/

// You can also get an fingerprint by the header of an table with, which will be return as an integer
$fingerprint = $horizontalTable->getFingerprint();

// This can be used to identify tables by the header for the next time



$parsedHTMLTable = array(
    array(
        "Cell1 in Row1",
        "Cell2 in Row1",
        "Cell3 in Row1"
    ),
    array(
        "Cell1 in Row2",
        "Cell2 in Row2",
        "Cell3 in Row2"
    ),
    array(
        "Cell1 in Row3",
        "Cell2 in Row3",
        "Cell3 in Row3"
    )
);

// __constructor($parsedHTMLTable)
$verticalTable = \TableDude\Converter\VerticalTable($parsedHTMLTable);

/*
You can also set the index to -1, then the last column will be selected
*/
$verticalTable->setHeaderColumnIndex(0);

// Sets the first column array in parsedHTMLTable as header column
/*
Header would now be
array(
    "Cell1 in Row1",
    "Cell1 in Row2",
    "Cell1 in Row3"
)

// convertes the array
$groupedTable = $verticalTable->getGroupedTable();

/*
This would return

array(
    array(
        "Cell1 in Row1" => "Cell2 in Row1",
        "Cell1 in Row2" => "Cell2 in Row2",
        "Cell1 in Row3" => "Cell2 in Row3"
    ),
        array(
        "Cell1 in Row1" => "Cell3 in Row1",
        "Cell1 in Row2" => "Cell3 in Row2",
        "Cell1 in Row3" => "Cell3 in Row3"
    ),
)

*/

// You can also get an fingerprint by the header of an table with, which will be return as an integer
$fingerprint = $verticalTable->getFingerprint();

// This can be used to identify tables by the header for the next time



$parsedHTMLTable = array(
    array(
        "Cell1 in Row1",
        "Cell2 in Row1",
        "Cell3 in Row1"
    ),
    array(
        "Cell1 in Row2",
        "Cell2 in Row2",
        "Cell3 in Row2"
    ),
    array(
        "Cell1 in Row3",
        "Cell2 in Row3",
        "Cell3 in Row3"
    )
);

// __constructor($parsedHTMLTable)
$mixedTable = \TableDude\Converter\VerticalTable($parsedHTMLTable);

/*
You can also set the index to -1, then the last row will be selected
*/
$mixedTable->setHeaderRowIndex(0);
$mixedTable->setHeaderColumnIndex(0);

// Nests the vertical Table in the horizontal Table
$mixedTable->nestVerticalTableInHorizontalTable();


// Nests the horizontal Table in the vertical Table
// $mixedTable->nestHorizontalTableInVerticalTable();


// convertes the array
$groupedTable = $mixedTable->getGroupedTable();

/*
This would return

array(
    "Cell1 in Row1" => array(
        "Cell1 in Row2" => "Cell2 in Row2",
        "Cell1 in Row3" => "Cell2 in Row3"
    ),
    "Cell2 in Row1" => array(
        "Cell1 in Row2" => "Cell3 in Row2",
        "Cell1 in Row3" => "Cell3 in Row3"
    ),
)

*/

// You can also get an fingerprint by the header of an table with, which will be return as an integer

// In the mixed table there are different types of fingerprints
// There are 6 constants to get the fingerprint of the header you wish

/*
// TD_FINGERPRINT_VERTICAL_HEADER
Would take fingerprint of 
array(
    "Cell1 in Row2",
    "Cell1 in Row3"
)
The cell which overlaps with the horizontal header is not 


$array = array(
    array("1", "2", "3"),
    array("4", "5", "6")
);

$swappedArray = \TableDude\Tools\ArrayTool::swapArray($array);

/*
Content of $swappedArray
array(
    array("1", "4"),
    array("2", "5"),
    array("3", "6")
)
*/


$array = array(
    array("1", "2", "3"),
    array("4", "5", "6", "7")
);

$longestRow = \TableDude\Tools\ArrayTool::countLongestRowOfArrayTable($array);

/*
$longestRow would be 4 in this case
*/