在PHP中解析格式不正确的HTML [英] Parsing of badly formatted HTML in PHP
问题描述
在我的代码中,我使用openoffice将一些样式化的xls文档转换为html。
然后我使用 xml_parser_create
来分析表格。
问题在于openoffice创建了oldschool html,并且未封闭< BR>
和< HR>
标签,它不会创建文档类型,也不会引用属性< TABLE WIDTH = 4>
。
In my code I convert some styled xls document to html using openoffice.
I then parse the tables using xml_parser_create
.
The problem is that openoffice creates oldschool html with unclosed <BR>
and <HR>
tags, it doesn't create doctypes and don't quote attributes <TABLE WIDTH=4>
.
我知道的PHP解析器不喜欢这样,并产生XML格式错误。我当前的解决方案是在解析文件之前对文件运行一些正则表达式,但这并不好也不快。
The php parsers I know off don't like this, and yield xml formatting errors. My current solution is to run some regexes over the file before I parse it, but this is neither nice nor fast.
你知道一个(希望包含的)php解析器,那不关心这类错误?或者可能是一个快速的方法来修复一个'坏'的HTML?
Do you know a (hopefully included) php-parser, that doesn't care about these kinds of mistakes? Or perhaps a fast way to fix a 'broken' html?
推荐答案
解决方案修复 HTMLPurifier (引用):
HTML Purifier是用PHP编写的符合标准的
HTML滤镜库。
HTML Purifier不仅会删除
所有恶意代码(更为人熟知的是
XSS),它还包含经过全面审计的
安全但宽松的白名单,它
也将确保您的文档符合标准
另一种想法可能是尝试使用 DOMDocument :: loadHTML $>加载HTML
$ b
An alternative idea might be to try loading your HTML with DOMDocument::loadHTML
(quoting) :
该函数解析HTML中包含的
在字符串源中。与加载
XML不同, HTML不必是
格式才能加载。
如果您尝试从文件加载HTML,请参阅 DOMDocument :: loadHTMLFile
。
And if you're trying to load HTML from a file, see DOMDocument::loadHTMLFile
.
这篇关于在PHP中解析格式不正确的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!