PHP Function to Get Array of HTML Elements from HTML
Document Object ModelThe function uses Document Object Model to get array of HTML elements of any particular tag element from the HTML. You can also get values from specific tags that have particular attribute and value.
Function Definition
public static function getHtmlByDOM($html, $element, $attrchk='class', $ca=NULL)
{
$html = mb_convert_encoding($html, "HTML-ENTITIES", 'UTF-8');
$dom = new domDocument;
$dom->preserveWhiteSpace = false;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$contents = $dom->getElementsByTagName($element); // Array of Content
foreach($contents as $content)
{
if ($ca != NULL && $content->getAttribute($attrchk) == $ca)
{
$html_arr[] = $dom->saveHtml($content);
}
else if ($ca == NULL)
{
$html_arr[] = $dom->saveHTML($content);
}
}
return $html_arr;
}