1
This commit is contained in:
@@ -300,8 +300,10 @@ class SkuController extends BaseController
|
||||
$html
|
||||
);
|
||||
|
||||
// 2️⃣ 关键:HTML 清洗(适配 wangEditor)
|
||||
$html = $this->cleanWordHtmlForWangEditor($html);
|
||||
// // 2️⃣ 关键:HTML 清洗(适配 wangEditor)
|
||||
// $html = $this->cleanWordHtmlForWangEditor($html);
|
||||
// 4. 清洗 HTML(关键)
|
||||
$html = $this->cleanDocHtml($html);
|
||||
|
||||
return $html;
|
||||
}
|
||||
@@ -317,6 +319,44 @@ class SkuController extends BaseController
|
||||
@rmdir($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清洗 doc/docx 转出的 HTML,适配富文本编辑器
|
||||
*/
|
||||
function cleanDocHtml(string $html): string
|
||||
{
|
||||
libxml_use_internal_errors(true);
|
||||
|
||||
$dom = new DOMDocument('1.0', 'UTF-8');
|
||||
$dom->loadHTML(
|
||||
'<?xml encoding="UTF-8">' . $html,
|
||||
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
|
||||
);
|
||||
|
||||
$xpath = new DOMXPath($dom);
|
||||
|
||||
// 1. 删除 head / meta / style / script
|
||||
foreach (['head', 'meta', 'style', 'script', 'xml'] as $tag) {
|
||||
foreach ($xpath->query("//{$tag}") as $node) {
|
||||
$node->parentNode?->removeChild($node);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 只取 body 内容
|
||||
$body = $dom->getElementsByTagName('body')->item(0);
|
||||
if ($body) {
|
||||
$innerHtml = '';
|
||||
foreach ($body->childNodes as $child) {
|
||||
$innerHtml .= $dom->saveHTML($child);
|
||||
}
|
||||
} else {
|
||||
$innerHtml = $dom->saveHTML();
|
||||
}
|
||||
|
||||
libxml_clear_errors();
|
||||
|
||||
return trim($innerHtml);
|
||||
}
|
||||
|
||||
function cleanWordHtmlForWangEditor(string $html): string
|
||||
{
|
||||
// 1. 移除 Word 专属标签
|
||||
|
||||
Reference in New Issue
Block a user