![](/img/trans.png)
[英]PHP Warning DOMDocument::loadHTML(): Tag section invalid in Entity on line 10
[英]PHP How to avoid this warning: DOMDocument::loadHTML(): Invalid char in CDATA
我正在嘗試從 web 服務收集一些信息,但我遇到了頁面的 CDATA 部分的問題,因為當我使用這樣的東西時一切正常:
$url = 'http://www.example.com';
$content = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($content);
foreach($doc->getElementsByTagName('h3') as $subtitle) {
echo $subtitle->textContent; //The output is the Subtitle/s.
}
但是當頁面包含 CDATA 部分時,$doc->loadHTML($content) 行上的此錯誤會出現問題。
Warning: DOMDocument::loadHTML(): Invalid char in CDATA
我在這里看到了一個我試圖實施但沒有成功的解決方案。
function sanitize_html($content) {
if (!$content) return '';
$invalid_characters = '/[^\x9\xa\x20-\xD7FF\xE000-\xFFFD]/';
return preg_replace($invalid_characters,'', $content);
}
$url = 'http://www.example.com';
$content = file_get_contents($url);
$cleanContent = sanitize_html($content);
$doc = new DOMDocument();
$doc->loadHTML($cleanContent); //Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity
但我得到了另一個錯誤:
Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity
什么是處理頁面的 CDATA 部分的好方法? 問候。
嘗試添加PCLZIP
負載之前IOFactory
如圖所示:
require_once '/Classes/PHPExcel.php';
\PHPExcel_Settings::setZipClass(\PHPExcel_Settings::PCLZIP);
解決方案是-用&
替換&
符號&
或者,如果您必須具有該&,則可以將其包含在以下內容中: <![CDATA[ - ]]>
添加 libxml_use_internal_errors(true) 和 libxml_clear_errors() 這個工作對我來說請點擊下面查看代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.