preserveWhiteSpace = false; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); return $dom; } // 01. Function to get array of HTML elements public static function get_html($html, $tag, $attr = null, $attr_value = null) { $dom = self::get_dom($html); $contents = $dom->getElementsByTagName($tag); // Array of Content if ($contents->length == 0) { return false; } foreach ($contents as $content) { if ($attr != null) { if ($content->getAttribute($attr) == $attr_value) { $html_arr[] = $dom->saveHtml($content); } } elseif ($attr == null) { $html_arr[] = $dom->saveHTML($content); } } return $html_arr; } // 02. Function to get array of values public static function get_value($html, $tag, $attr = null, $attr_value = null) { $dom = self::get_dom($html); $contents = $dom->getElementsByTagName($tag); // Array of Content if ($contents->length == 0) { return false; } foreach ($contents as $content) { if ($attr != null) { if ($content->getAttribute($attr) == $attr_value) { $values[] = $content->nodeValue; } } elseif ($attr == null) { $values[] = $content->nodeValue; } } return $values; } // 03. Function to get array of attribute values public static function get_attr($html, $tag, $target, $attr = null, $attr_value = null) { $dom = self::get_dom($html); $contents = $dom->getElementsByTagName($tag); // Array of Content if ($contents->length == 0) { return false; } foreach ($contents as $content) { if ($attr != null) { if ($content->getAttribute($attr) == $attr_value) { $attr_values[] = $content->getAttribute($target); } } elseif ($attr == null) { $attr_values[] = $content->getAttribute($target); } } return $attr_values; } // 04. Function to remove all attributes from HTML except some public static function rem_attr($html, $noremove = array()) { $dom = self::get_dom($html); $xpath = new \DOMXPath($dom); $nodes = $xpath->query('//@*'); foreach ($nodes as $node) { if (!in_array($node->nodeName, $noremove)) { $node->parentNode->removeAttribute($node->nodeName); } } return $dom->saveHTML(); } // 05. Function to add attribute to specific tag public static function add_attr($html, $tag, $attr, $attr_value) { $dom = self::get_dom($html); $nodes = $dom->getElementsByTagName($tag); if (is_array($attr_value)) { foreach ($nodes as $k => $node) { $node->setAttribute($attr, $attr_value[$k]); } } else { foreach ($nodes as $node) { $node->setAttribute($attr, $attr_value); } } return $dom->saveHTML(); } // 06. Function to remove complete element public static function rem_element($html, $tag, $attr = null, $attr_value = null) { $dom = self::get_dom($html); $list = $dom->getElementsByTagName($tag); for ($i = $list->length; --$i >= 0;) { $node = $list->item($i); if ($attr != null) { if ($node->getAttribute($attr) == $attr_value) { $node->parentNode->removeChild($node); } } elseif ($attr == null) { $node->parentNode->removeChild($node); } } return $dom->saveHTML(); } // 07. Function to get array of names of unique tags in html public static function get_tags($html) { $dom = self::get_dom($html); $elements = $dom->getElementsByTagName('*'); $tag_names[] = array(); foreach ($elements as $element) { if (!in_array($element->tagName, $tag_names)) { $tag_names[] = $element->tagName; } } return $tag_names; } // 08. public static function get_html_by_value($html, $tag, $value = '') { $dom = self::get_dom($html); $contents = $dom->getElementsByTagName($tag); // Array of Content if ($contents->length == 0) { return false; } foreach ($contents as $content) { if (!empty($value)) { if (strpos($content->nodeValue, $value) !== false) { $html_arr[] = $dom->saveHtml($content); } } elseif (empty($value)) { $html_arr[] = $dom->saveHTML($content); } } return $html_arr; } }