Реализация (Отобразить)
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- /**
- *
- * @property-read string $text
- * @property-read string $html
- * @property-read string $className
- * @property-read array $classList
- * @property-read array $attrList
- * @property-read object $data
- */
- class parseHtmlElement extends DOMElement {
- private $__data = NULL;
- private static $__fnFilter = NULL;
- public function __toString() {
- return $this->isBody ? $this->html : $this->ownerDocument->saveHTML($this);
- }
- public function __get($name)
- {
- switch($name)
- {
- case('className') :
- case('classList') :
- case('attrList') :
- {
- foreach($this->attributes as $name => $attrNode) {
- $attributes[$name] = $attrNode->value;
- }
- return $attributes;
- }
- case('text') :
- return $this->textContent;
- case('html') :
- {
- $html = '';
- foreach($this->childNodes as $child) {
- $html .= $child->ownerDocument->saveHtml($child);
- }
- return $html;
- }
- case('data') :
- {
- {
- $this->__data = new stdClass();
- foreach($this->attrList as $name => $value)
- {
- {
- // Преобразование в верблюжью нотацию (camelCase).
- $this->__data->{$name} = $value;
- }
- }
- }
- return $this->__data;
- }
- default:
- return $this->getAttribute($name);
- }
- }
- public static function filter($item)
- {
- }
- public function xPath($query, $filter = NULL)
- {
- {
- self::$__fnFilter = $filter;
- }
- if($this->isBody)
- foreach($this->xpath->query($query, $this) as $item) {
- $item->xpath = $this->xpath;
- $tesult[] = $item;
- }
- return $tesult;
- }
- }
- /**
- * @param string $html
- * @param string $encoding
- * @return parseHtmlElement
- */
- function parseHtml($html, $encoding = 'utf-8')
- {
- $doc = new DOMDocument('1.0', $encoding);
- $doc->registerNodeClass('DOMElement', 'parseHtmlElement');
- $doc->normalize();
- $xpath = new DOMXPath($doc);
- $xpath->registerNamespace("php", "http://php.net/xpath");
- $xpath->registerPHPFunctions('parseHtmlElement::filter');
- $documentElement = $isBody
- ? $xpath->query('//body[1]')->item(0)
- : $doc->documentElement;
- $documentElement->isBody = $isBody;
- $documentElement->xpath = $xpath;
- return $documentElement;
- }