问题描述
我有以下代码在本地主机上工作正常:
I had the following code which works fine on my localhost:
$document = new DOMDocument();
$document->loadHTML($p_result);
$form = $document->getElementsByTagName('form')->item(0);
// Code continues using the $form variable
在外部服务器上更新, loadHTML()
失败并发出此警告。
After same code get updated on outside server, loadHTML()
failed and issued this warning.
Warning: DOMDocument::loadHTML() expects parameter 1 to be a valid path, string given in path/name/to/script.php
返回NULL而不是对象,所以代码很快就会导致致命错误。
Returned NULL as well instead of object so code pretty soon gets to the fatal error.
请注意, $ p_result
在外部服务器和本地主机上是绝对相同的。
Note that the contents of $p_result
is absolutely the same on outside server and on my localhost.
但是为什么会显示这种警告,为什么它不工作?
But why does it displays that kind of warning and why does it not work?
不是 loadHTML()
expects参数1是一个字符串在第一位?
Doesn't it loadHTML()
expects argument 1 to be a string in the first place?
为什么这个方法希望参数1成为有效的路径
?
只是为了表明我不是调用 loadHTMLFile()
,我打电话给$ code loadHTML() 。
Just to make it clear I'm not calling loadHTMLFile()
, I'm calling loadHTML()
.
谢谢。
推荐答案
您受到的影响。该问题仅在PHP 5.6.8和5.6.9中出现。很可能您已经在服务器上影响了PHP版本,并且本地主机上出现了无bug版本。
You're affected by one of PHP bugs. The issue was present only in PHP 5.6.8 and 5.6.9. Most likely you have affected PHP version on the server, and bug-free version on your localhost.
该错误本身禁止加载的HTML文档中的所有空字符,所以作为一种解决方法,您可以尝试在进一步解析之前删除那些(实际上不需要的)字符。
The bug itself forbids all null characters in HTML document you're loading, so as a workaround you may try to remove those (actually not needed) characters before further parsing.
$document = new DOMDocument();
$p_result_without_null_chars = str_replace("\0", '', $p_result)
$document->loadHTML($p_result_without_null_chars);
这篇关于PHP DOM loadHTML()方法异常警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!