WIN-2308041133\Administrator 4 months ago
parent
commit
587649a678
1 changed files with 72 additions and 0 deletions
  1. 72 0
      app/admin/model/article/Article.php

+ 72 - 0
app/admin/model/article/Article.php

@@ -129,6 +129,8 @@ class Article extends BaseModel
                 $res = true;
                 $res = true;
             } else{
             } else{
                 @file_put_contents('quanju.txt', $content . "-789\r\n", 8);
                 @file_put_contents('quanju.txt', $content . "-789\r\n", 8);
+                $content = self::fixArticleContent($content);
+                @file_put_contents('quanju.txt', $content . "-超级变换形态\r\n", 8);
                 $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
                 $res = ArticleContent::where('nid', $id)->update(['content' => $content]);
             }
             }
             if ($res !== false) $res = true;
             if ($res !== false) $res = true;
@@ -137,7 +139,77 @@ class Article extends BaseModel
         }
         }
         return $res;
         return $res;
     }
     }
+    public static function fixArticleContent($html) {
+        // 1. 去除多余的转义反斜杠
+        $fixed = stripslashes($html);
 
 
+        // 2. 修复图片标签的格式问题
+        $fixed = preg_replace_callback('/<img\s+([^>]*)>/i', function($matches) {
+            $attrs = $matches[1];
+
+            // 提取src属性
+            preg_match('/src\s*=\s*["\']([^"\']+)["\']/i', $attrs, $srcMatch);
+            $src = $srcMatch[1] ?? '';
+
+            // 提取宽度和高度(如果有)
+            preg_match('/width\s*:\s*([\d.]+px)/i', $attrs, $widthMatch);
+            preg_match('/height\s*:\s*([\d.]+px)/i', $attrs, $heightMatch);
+            $width = $widthMatch[1] ?? '';
+            $height = $heightMatch[1] ?? '';
+
+            // 提取现有样式(如果有)
+            preg_match('/style\s*=\s*["\']([^"\']+)["\']/i', $attrs, $styleMatch);
+            $existingStyle = $styleMatch[1] ?? '';
+
+            // 构建新属性
+            $newAttrs = '';
+
+            // 保留所有有效属性(排除错误的=属性)
+            preg_match_all('/(\w+)\s*=\s*["\'][^"\']*["\']/i', $attrs, $validAttrs);
+            foreach ($validAttrs[0] as $attr) {
+                // 跳过错误的=属性
+                if (!strpos($attr, '="')) {
+                    continue;
+                }
+
+                // 跳过将被替换的width/height属性
+                $attrName = strtolower(trim(explode('=', $attr)[0]));
+                if ($attrName === 'width' || $attrName === 'height') {
+                    continue;
+                }
+
+                $newAttrs .= ' ' . $attr;
+            }
+
+            // 构建样式属性
+            $style = trim($existingStyle);
+            if (!empty($width) || !empty($height)) {
+                $newStyle = '';
+                if (!empty($width)) $newStyle .= "width: $width;";
+                if (!empty($height)) $newStyle .= "height: $height;";
+
+                if (!empty($style)) {
+                    // 合并新样式和现有样式
+                    $style = rtrim($style, ';') . ';' . $newStyle;
+                } else {
+                    $style = $newStyle;
+                }
+            }
+
+            // 添加样式属性(如果有)
+            if (!empty($style)) {
+                $newAttrs .= ' style="' . htmlspecialchars($style) . '"';
+            }
+
+            return '<img' . $newAttrs . '>';
+        }, $fixed);
+
+        // 3. 修复可能被破坏的段落标签
+        $fixed = preg_replace('/<p>\s*<\/p>/', '', $fixed); // 移除空段落
+        $fixed = preg_replace('/(<p[^>]*>)\s*(<\/p>)/', '$1$2', $fixed); // 移除段落内的空白
+
+        return $fixed;
+    }
     public static function merchantPage($where = [])
     public static function merchantPage($where = [])
     {
     {
         $model = new self;
         $model = new self;