Apr
11
Useful HTMLParse Class for PHP
Filed Under (Code) by Mystalia on 11-04-2009
Tagged Under : file_get_contents, help, html, htmlparse, php, read, script, tutorial
class HTMLParse{ // the missing InnerHTML function! function innerHTML($node){ $doc = new DOMDocument(); foreach ($node->childNodes as $child){ $doc->appendChild($doc->importNode($child, true));} $result = $doc->saveHTML(); return $result;} // Get the inner html of all elements inside tag from the source (input). function GetInnerArray($tag, $input){ $doc = new DOMDocument(); @$doc->loadHTML($input); $dataset = $doc->getElementsByTagName($tag); $stringarr = array(); foreach( $dataset as $row ){ array_push($stringarr, trim($this->innerHTML($row)));} return $stringarr;} // Get the inner html of all elements inside tag where an attribute exists with the value. function GetInnerArrayFilter($input, $tag, $attribute, $value){ $doc = new DOMDocument(); @$doc->loadHTML($input); $dataset = $doc->getElementsByTagName($tag); $stringarr = array(); foreach( $dataset as $row ){ if($row->getAttribute($attribute) == $value){ array_push($stringarr, trim($this->innerHTML($row)));}} return $stringarr;} // Get attribute of tags. function GetTagAttribute($input, $tag, $attribute){ $doc = new DOMDocument(); @$doc->loadHTML($input); $dataset = $doc->getElementsByTagName($tag); $stringarr = array(); foreach( $dataset as $row ){ array_push($stringarr, $row->getAttribute($attribute));} return $stringarr;} // Get attribute of tags where the tag attrib has the value. function GetTagAttributeFilter($input, $tag, $attribute, $qattrib, $value){ $doc = new DOMDocument(); @$doc->loadHTML($input); $dataset = $doc->getElementsByTagName($tag); $stringarr = array(); foreach( $dataset as $row ){ if($row->getAttribute($qattrib) == $value){ array_push($stringarr, $row->getAttribute($attribute));}} return $stringarr;}} ?>
Mostly written by me.