/**
* print out string as html encoded using `htmlspecialchars()` function.
*/
function printDebug($string)
{
echo '<pre>' . htmlspecialchars(print_r($string, true), ENT_QUOTES) . '</pre>' . PHP_EOL;
}
echo '<h3>original</h3>' . PHP_EOL;
printDebug($str);
echo '<hr>' . PHP_EOL;
/**
* Get inner content of HTML table.
*
* @param string $string
* @param string $getElementName The HTML element name to get.
* @param string|false $requiredAttribute Set to false for not validate, set the attribute name to validate.
* @param string|false $requiredAttributeValue Set to false for not validate, set the attribute value to validate.
* @return string
* @throw exception on failed to validate or get HTML.
*/
function getHTMLTableInnerContent($string, $getElementName = 'tbody', $requiredAttribute = false, $requiredAttributeValue = false)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
// '<?xml encoding="utf-8" ?\>' คือเพื่อให้มันแสดงภาษาไทย. ( https://stackoverflow.com/a/8218649/128761 )
// LIBXML_HTML_NODEFDTD คือเพื่อให้ไม่ต้องวุ่นวายกับ doctype. ( http://php.net/manual/en/libxml.constants.php )
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $string, LIBXML_HTML_NODEFDTD);
$table = $dom->getElementsByTagName('table')->item(0);
if (is_object($table)) {
$tbody = $table->getElementsByTagName($getElementName)->item(0);
if (
(
// if required attribute and its value are false (not required).
$requiredAttribute === false &&
$requiredAttributeValue === false
) ||
(
// if required attribute and its value are string, it must be validate.
is_string($requiredAttribute) &&
is_string($requiredAttributeValue) &&
$tbody->hasAttribute($requiredAttribute) &&
$tbody->getAttribute($requiredAttribute) === $requiredAttributeValue
)
) {
return $tbody->ownerDocument->saveHTML($tbody);
} else {
// อาจเปลี่ยน throw error เป็นอย่างอื่นก็ได้ เช่น return false;
throw new \Exception('Invalid html content (' . $getElementName . ' contain no attribute or wrong value for this attribute.).');
}
unset($tbody);
} else {
// อาจเปลี่ยน throw error เป็นอย่างอื่นก็ได้ เช่น return false;
throw new \Exception('Invalid html content (table).');
}
unset($table);
}
/**
* Make string into array format.
* @param string $string
* @return string|array Return array on success, return original content on failed.
*/
function makeArray($string)
{
if (is_string($string)) {
$array = array(
array(
0 => $string,
)
);
return $array;
}
return $string;
}
echo '<h3>get html table inner content</h3>' . PHP_EOL;
echo printDebug(getHTMLTableInnerContent($str, 'tbody', 'parser-repeat', 'master_data'));
echo '<h4>make it array you want.</h4>' . PHP_EOL;
echo printDebug(makeArray(getHTMLTableInnerContent($str, 'tbody', 'parser-repeat', 'master_data')));