Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DOMDocument saveHTML未返回“IMG”、“INPUT”的正确HTML标准_Html_Simplexml_Domdocument_W3c Validation_Phpquery - Fatal编程技术网

DOMDocument saveHTML未返回“IMG”、“INPUT”的正确HTML标准

DOMDocument saveHTML未返回“IMG”、“INPUT”的正确HTML标准,html,simplexml,domdocument,w3c-validation,phpquery,Html,Simplexml,Domdocument,W3c Validation,Phpquery,我非常喜欢PHP库内容解析器,因为它非常类似于jQuery,虽然使用来提取标记,但我注意到了一个bug,它使用快速关闭事件而不是 我注意到这个bug也出现在DOMDocument和phpQuery中 我编写了一个简单的类来转储一个简单的html文档 它返回 $php_query_document = new DOMDocument('UTF-8', '1.0'); $php_query_document->formatOutput = true; $php_query_document-&

我非常喜欢PHP库内容解析器,因为它非常类似于jQuery,虽然使用来提取标记,但我注意到了一个bug,它使用快速关闭事件而不是

我注意到这个bug也出现在DOMDocument和phpQuery中

我编写了一个简单的类来转储一个简单的html文档

它返回

$php_query_document = new DOMDocument('UTF-8', '1.0');
$php_query_document->formatOutput = true;
$php_query_document->preserveWhiteSpace = true;
$php_query_document->loadHTML($sample_document_string);

$php_query_document_string = $php_query_document->saveHTML();

echo $php_query_document_string;
我得到以下警告和异常,即使我的原始标记可以使用SimpleXMLElement

由于该元素没有关闭事件

TL:DR警告:SimpleXMLElement::_构造:实体:第1行:解析器错误:标记img第1行中的数据过早结束

我怎样才能解决这个问题?我确实有一些想法,但最好是

我想要一个解决方案,在这个解决方案中,我可以使用我知道元素类型的正则表达式来替换/>,反之亦然。 保存HTML的DOMDocument类可能是一个扩展DOMDocument以继承其他功能的类。 如果使用而不是,则会得到有效的XML

如果需要,您可以剥离xml声明行

我刚刚意识到您希望find方法返回正确的XML。因此,如果这意味着您必须更改实现该方法的类,我不确定我的上述建议是否有帮助

也许你可以做一些有点复杂的事情,比如:

Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Premature end of data in tag img line 1 in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Warning: SimpleXMLElement::__construct(): <img src="png_file.png" alt="png_file" id="png_file"> in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Warning: SimpleXMLElement::__construct(): ^ in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php:17 Stack trace: #0 F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php(17): SimpleXMLElement->__construct('<img src="png_f...') #1 {main} thrown in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17
这假定$node是的某个实现,我怀疑它是。这样做的目的是要求包含该节点的$node->ownerDocument仅将该特定节点保存为XML

我不一定推荐的另一种可能性是,在解析时,通过将以下libxml选项传递给构造函数,让SimpleXML变得宽松:

$node = $php_query_document->find('img#png_file');
$simple_doc = new SimpleXMLElement( $node->ownerDocument->saveXML( $node ) );

这将在解析内容时抑制libxml错误。是基础XML解析器,SimpleXML和DOMDocument等使用。

是否有正则表达式来查找单词[0-9];例如13?因为saveXML会插入这些随机字符references@Killrawr我认为您没有正确地使用SimpleXML,因为我认为您所指的是进行var_转储的输出。您想用SimpleXML实现什么?请编辑您的问题,或者最好用您面临的新问题开始新问题。如果您仍然需要正则表达式方面的帮助,请同时开始一个关于正则表达式问题的全新问题。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Sample Document</title>
</head>
<body>
<span class="text_element">This is some Sample Text</span>
<img src="png_file.png" alt="png_file" id="png_file">
</body>
</html>
$simple_doc = new SimpleXMLElement((string) $php_query_document->find('img#png_file'));
Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Premature end of data in tag img line 1 in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Warning: SimpleXMLElement::__construct(): <img src="png_file.png" alt="png_file" id="png_file"> in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Warning: SimpleXMLElement::__construct(): ^ in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php:17 Stack trace: #0 F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php(17): SimpleXMLElement->__construct('<img src="png_f...') #1 {main} thrown in F:\xampp\htdocs\Test_Code\phpquery_test_items\index.php on line 17
$node = $php_query_document->find('img#png_file');
$simple_doc = new SimpleXMLElement( $node->ownerDocument->saveXML( $node ) );
$simple_doc = new SimpleXMLElement(
    (string) $php_query_document->find('img#png_file'), 
    LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL
);