定制 csskevin/tabledude 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

csskevin/tabledude

Composer 安装命令:

composer require csskevin/tabledude

包简介

Helps you processing HTML Tables with PHP to save them for instance as associative arrays.

README 文档

README

TableDude parses, converts and analyses HTML Tables. This software is written in plain PHP without any external libraries (except PHPUnit for testing).

Compatibility

You can use this software for PHP version >= 7

It might work with older versions, but I haven't tested it.

Quick Start

To install this software you can either use composer or install it manually.

Composer way

composer require csskevin/tabledude

<?php
// If you are working with composer
require __DIR__ . '/vendor/autoload.php';
?>

Manual way

git clone https://github.com/csskevin/TableDude.git

<?php
// If you installed this software manual
require __DIR__ . "/<pathToTableDude>/src/autoload.php";
?>

Examples

require __DIR__ . "/../src/autoload.php";

// 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";
    }
}

You can also identify table headers by getting their fingerprint.

require __DIR__ . "/../src/autoload.php";

// 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);
    }
}

Testing

This software was tested with PHPUnit. The composer.json includes a test script.

composer install

composer run-script test

Documentation

There are four main components:

  • Parser (This parses HTML Tables to PHP Arrays)
  • Converter (Let's you convert and group PHP Arrays)
  • Analysis (Analyzes a table)
  • Tools (Some tools to modify the tables)

Parser

\TableDude\Parser\SimpleParser

$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"
        )
    )
)
*/

Converter

There are three different converter:

  • HorizontalTable
  • VerticalTable
  • MixedTable

\TableDude\Converter\HorizontalTable

This table converts out of a simple parsed HTML table, an associative array where the keys equals a defined header row.

The methods will be documented in the example below

$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

\TableDude\Converter\VerticalTable

This table converts out of a simple parsed HTML table, an associative array where the keys equals a defined header column.

The methods will be documented in the example below

$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

\TableDude\Converter\MixedTable

This table converts out of a simple parsed HTML table, an associative array where the keys equals a defined header row and header column.

The methods will be documented in the example below

$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 included

// TD_FINGERPRINT_HORIZONTAL_HEADER
Would take fingerprint of 
array(
    "Cell2 in Row1",
    "Cell3 in Row2"
)
The cell which overlaps with the vertical header is not included


// TD_FINGERPRINT_MIXED_HEADER
Would take fingerprint of 
array(
    array(
        "Cell1 in Row2",
        "Cell1 in Row3"
    ),
    array(
        "Cell2 in Row1",
        "Cell3 in Row2"
    )
)
The cell which overlaps with the vertical header is not included

------

The same as above but it includes the cell, which overlaps with the horizontal and the vertical header

// TD_FINGERPRINT_VERTICAL_HEADER_WITH_CROSSED_CELL
// TD_FINGERPRINT_HORIZONTAL_HEADER_WITH_CROSSED_CELL
// TD_FINGERPRINT_MIXED_HEADER_WITH_CROSSED_CELL
*/

$fingerprint = $mixedTable->getFingerprint();

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

Tools

\TableDude\Tools\ArrayTool

public static function swapArray($array);

Swaps an array

$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")
)
*/

public static function countLongestRowOfArrayTable($array);

Returns the length of the longest row

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

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

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

public static function validateVerticalTable($table);

Validates, wheter a vertical table is valid.

public static function validateHorizontalTable($table);

Validates, wheter a horizontal table is valid.

public static function validateMixedTable($table);

Validates, wheter a mixed table is valid.

Analysis

\TableDude\Analysis\HeaderAnalyzation

public static function getFingerPrintOfArray($array)

Returns a fingerprint of the array. This is very useful for identifying tables by the header.

csskevin/tabledude 适用场景与选型建议

csskevin/tabledude 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.17k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 csskevin/tabledude 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 csskevin/tabledude 我们能提供哪些服务?
定制开发 / 二次开发

基于 csskevin/tabledude 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-08