This is a HTML parser class, used to parse HTML and XML. One of the unique features of this class is that it supports the innerHTML property.
php
/**
* HTML/XML Parser Class
*
* This is a helper class that is used to parse HTML and XML. A unique feature of this parsing class
* is the fact that it includes support for innerHTML (which isn't easy to do).
*
* @author Dennis Pallett
* @copyright Dennis Pallett 2006
* @package HTML_Parser
* @version 1.0
*/
// Helper Class
// To parse HTML/XML
Class HTML_Parser {
// Private properties
var $_parser;
var $_tags = array();
var $_html;
var $output = array();
var $strXmlData;
var $_level = 0;
var $_outline;
var $_tagcount = array();
var $xml_error = false;
var $xml_error_code;
var $xml_error_string;
var $xml_error_line_number;
function get_html () {
return $this->_html;
}
function parse($strInputXML) {
$this->output = array();
// Translate entities
$strInputXML = $this->translate_entities($strInputXML);
$this->_parser = xml_parser_create ();
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_object($this->_parser,$this);
xml_set_element_handler($this->_parser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->_parser, "tagData");
$this->strXmlData = xml_parse($this->_parser,$strInputXML );
if (!$this->strXmlData) {
$this->xml_error = true;
$this->xml_error_code = xml_get_error_code($this->_parser);
$this->xml_error_string = xml_error_string(xml_get_error_code($this->_parser));
$this->xml_error_line_number = xml_get_current_line_number($this->_parser);
return false;
}
return $this->output;
}
function tagOpen($parser, $name, $attr) {
// Increase level
$this->_level++;
// Create tag:
$newtag = $this->create_tag($name, $attr);
// Build tag
$tag = array("name"=>$name,"attr"=>$attr, "level"=>$this->_level);
// Add tag
array_push ($this->output, $tag);
// Add tag to this level
$this->_tags[$this->_level] = $tag;
// Add to HTML
$this->_html .= $newtag;
// Add to outline
$this->_outline .= $this->_level . $newtag;
}
function create_tag ($name, $attr) {
// Create tag:
# Begin with name
$tag = '. strtolower($name) . ' ';
# Create attribute list
foreach ($attr as $key=>$val) {
$tag .= strtolower($key) . '="' . htmlentities($val) . '" ';
}
# Finish tag
$tag = trim($tag);
switch(strtolower($name)) {
case 'br':
case 'input':
$tag .= ' /';
break;
}
$tag .= '>';
return $tag;
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->output[count($this->output)-1]['tagData'])) {
$this->output[count($this->output)-1]['tagData'] .= $tagData;
} else {
$this->output[count($this->output)-1]['tagData'] = $tagData;
}
}
$this->_html .= htmlentities($tagData);
$this->_outline .= htmlentities($tagData);
}
function tagClosed($parser, $name) {
// Add to HTML and outline
switch (strtolower($name)) {
case 'br':
case 'input':
break;
default:
$this->_outline .= $this->_level . '' . strtolower($name) . '>';
$this->_html .= '' . strtolower($name) . '>';
}
// Get tag that belongs to this end
$tag = $this->_tags[$this->_level];
$tag = $this->create_tag($tag['name'], $tag['attr']);
// Try to get innerHTML
$regex = '%' . preg_quote($this->_level . $tag, '%') . '(.*?)' . preg_quote($this->_level . '' . strtolower($name) . '>', '%') . '%is';
preg_match ($regex, $this->_outline, $matches);
// Get innerHTML
if (isset($matches['1'])) {
$innerhtml = $matches['1'];
}
// Remove level identifiers
$this->_outline = str_replace($this->_level . $tag, $tag, $this->_outline);
$this->_outline = str_replace($this->_level . '' . strtolower($name) . '>', '' . strtolower($name) . '>', $this->_outline);
// Add innerHTML
if (isset($innerhtml)) {
$this->output[count($this->output)-1]['innerhtml'] = $innerhtml;
}
// Fix tree
$this->output[count($this->output)-2]['children'][] = $this->output[count($this->output)-1];
array_pop($this->output);
// Decrease level
$this->_level--;
}
function translate_entities($xmlSource, $reverse =FALSE) {
static $literal2NumericEntity;
if (empty($literal2NumericEntity)) {
$transTbl = get_html_translation_table(HTML_ENTITIES);
foreach ($transTbl as $char => $entity) {
if (strpos('&"', $char) !== FALSE) continue;
$literal2NumericEntity[$entity] = ''.ord($char).';';
}
}
if ($reverse) {
return strtr($xmlSource, array_flip($literal2NumericEntity));
} else {
return strtr($xmlSource, $literal2NumericEntity);
}
}
}
// To be used like this
$parser = new HTML_Parser;
$output = $parser->parse($html);
print_r ($output);
?>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경
