From ef3a9f10685d7c84cca386a67d7138ad02ae45dc Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Tue, 16 Dec 2025 14:59:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/SkuController.php | 79 +++++++++++++------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/app/admin/controller/SkuController.php b/app/admin/controller/SkuController.php index 8f420fe..0544f19 100644 --- a/app/admin/controller/SkuController.php +++ b/app/admin/controller/SkuController.php @@ -264,10 +264,8 @@ class SkuController extends BaseController return ''; } - // 1. 读取 docx - $phpWord = WordIOFactory::load($docxPath); + $phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath); - // 2. 导出 HTML $tmpDir = runtime_path('tmp/' . uniqid()); if (!is_dir($tmpDir)) { mkdir($tmpDir, 0755, true); @@ -275,48 +273,37 @@ class SkuController extends BaseController $tmpHtml = $tmpDir . '/index.html'; - $writer = WordIOFactory::createWriter($phpWord, 'HTML'); + $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); $writer->save($tmpHtml); $html = file_get_contents($tmpHtml); - // 3. 只取 body 内容(关键) - if (preg_match('/
]*>(.*?)<\/body>/is', $html, $m)) { - $html = $m[1]; - } - - // 4. 清洗 Word 垃圾样式(关键) - $html = $this->cleanWordHtml($html); - - // 5. 处理图片并上传 OSS + // 1️⃣ 上传图片到 OSS $uploadService = new UploadService(); - $html = preg_replace_callback( '/\s*<\/p>/i', '', $html);
+ // 3. 清理 span(保留内容)
+ while (preg_match('/]*>(.*?)<\/span>/is', $html)) {
+ $html = preg_replace('/]*>(.*?)<\/span>/is', '$1', $html);
+ }
- // 规范段落
- $html = str_replace([' ', ' ", " ', $html);
+ $html = preg_replace('/<\/div>/i', ' ]*src="([^"]+)"[^>]*>/i', '
', $html);
+
+ // 6. table 清洗
+ $html = preg_replace('/
]*>/i', '
', $html);
+ $html = preg_replace('/
]*>/i', ' ', $html);
+ $html = preg_replace('/ ]*>/i', ' ', $html);
+
+ // 7. 去除多余空白
+ $html = preg_replace('/\s+/u', ' ', $html);
+ $html = str_replace(['', '