Skip to content

Instantly share code, notes, and snippets.

@vihoangson
Last active December 8, 2023 02:36
Show Gist options
  • Save vihoangson/1d0c4d5b9de97d29d72ee1dda7256f6c to your computer and use it in GitHub Desktop.
Save vihoangson/1d0c4d5b9de97d29d72ee1dda7256f6c to your computer and use it in GitHub Desktop.
<?php
//============ ============ ============ ============
// Convert html table to aray
//
// @auth: ViHoangSon
// @since: 20160704092752
//
// Using:
// $contents = "<table><tr><td>Row 1 Column 1</td><td>Row 1 Column 2</td></tr><tr><td>Row 2 Column 1</td><td>Row 2 Column 2</td></tr></table>";
// $array = ConvertHelper::getdata($contents);
//============ ============ ============ ============
class ConvertHelper{
static function getdata($contents)
{
$DOM = new DOMDocument;
$DOM->loadHTML($contents);
$items = $DOM->getElementsByTagName('tr');
$return = array();
foreach ($items as $node) {
$return[] = self::tdrows($node->childNodes);
}
return $return;
}
static function tdrows($elements)
{
$str = array();
foreach ($elements as $element) {
$str[] = $element->nodeValue;
}
return $str;
}
}
?>
@gamersalpha
Copy link

Hello, i got an issue,

the TD are not in the array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment