Sai A Sai A
Updated date Feb 19, 2024
In this blog, we will learn how to convert HTML tables with custom classes into arrays using PHP. This blog offers two methods: using PHP's DOMDocument and DOMXPath classes or using the Simple HTML DOM Parser library.

Introduction:

HTML tables are a fundamental component of web development, used to display data in a structured format. Often, developers need to extract data from these tables for further processing. In PHP, converting an HTML table with custom classes to an array can be a common requirement, In this blog, we will explore multiple methods to achieve this task.

Method 1: Using DOMDocument and DOMXPath

We'll start with a method utilizing PHP's DOMDocument and DOMXPath classes. These classes allow us to navigate through HTML documents and extract specific elements based on XPath queries. Here's a simple program demonstrating this method:

$html = '<table class="custom-table">
            <tr>
                <th>Name</th>
                <th>Age</th>
            </tr>
            <tr>
                <td>John</td>
                <td>25</td>
            </tr>
            <tr>
                <td>Alice</td>
                <td>30</td>
            </tr>
        </table>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$data = [];
$rows = $xpath->query('//table[@class="custom-table"]/tr');

foreach ($rows as $row) {
    $rowData = [];
    foreach ($row->childNodes as $cell) {
        $rowData[] = $cell->nodeValue;
    }
    $data[] = $rowData;
}

print_r($data);

Output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => Age
        )

    [1] => Array
        (
            [0] => John
            [1] => 25
        )

    [2] => Array
        (
            [0] => Alice
            [1] => 30
        )

)

This method involves loading the HTML content into a DOMDocument object, then querying for table rows using XPath. We iterate through each row, extracting cell data and populating it into a multidimensional array.

Method 2: Using Simple HTML DOM Parser

Another approach employs the Simple HTML DOM Parser library, which provides a convenient way to parse HTML documents using CSS selectors. Here's how to do it:

$html = '<table class="custom-table">
            <tr>
                <th>Name</th>
                <th>Age</th>
            </tr>
            <tr>
                <td>John</td>
                <td>25</td>
            </tr>
            <tr>
                <td>Alice</td>
                <td>30</td>
            </tr>
        </table>';

include 'simple_html_dom.php'; // Include the Simple HTML DOM Parser library

$data = [];
$table = str_get_html($html)->find('table.custom-table', 0);
foreach ($table->find('tr') as $row) {
    $rowData = [];
    foreach ($row->find('td, th') as $cell) {
        $rowData[] = $cell->plaintext;
    }
    $data[] = $rowData;
}

print_r($data);

Output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => Age
        )

    [1] => Array
        (
            [0] => John
            [1] => 25
        )

    [2] => Array
        (
            [0] => Alice
            [1] => 30
        )

)

Here, we utilize the Simple HTML DOM Parser library to parse the HTML content. We select the table using CSS selector 'table.custom-table', then loop through rows and cells to extract the data into an array.

Conclusion:

In this blog, we have explored two methods to convert an HTML table with custom classes to an array in PHP. Whether you prefer the native DOMDocument approach or the simplicity of a third-party library like Simple HTML DOM Parser, both methods achieve the same result efficiently. 

Comments (0)

There are no comments. Be the first to comment!!!