Просмотр исходного кода

修改b端接口:添加项目、修改项目

15313670163 6 месяцев назад
Родитель
Сommit
7736ef5c48
1 измененных файлов с 36 добавлено и 9 удалено
  1. 36 9
      app/JsonRpc/NewsService.php

+ 36 - 9
app/JsonRpc/NewsService.php

@@ -6280,18 +6280,31 @@ class NewsService implements NewsServiceInterface
       if ($data['keyword'] == '') {
           //提取标题+内容中的关键词
           $data['keyword'] = $data['title'];
-          //  . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
           Jieba::init(); // 初始化 jieba-php
           Finalseg::init();
           $segList = Jieba::cut($data['keyword']);
           $segList1 = array_slice($segList, 0, 8);
           $data['keyword'] = implode(',', $segList1);
       }
-      if ($data['description'] == '') {
-        //提取内容中的描述
-        // var_dump(11111);
-        $data['description'] = mb_substr(str_replace(' ', '', strip_tags($data['introduce'])), 0, 100);
-      }
+        if ($data['description'] == '') {
+            //提取内容中的描述
+            // var_dump(11111);
+            //提取内容中的描述
+            $content = $data['introduce'];
+            // 去除 <style> 和 <script> 标签及其内容
+            $content = preg_replace('/<(style|script)[^>]*>.*?<\/\1>/is', '', $content);
+            // 去除所有 HTML 标签
+            $content = strip_tags($content);
+            // 去除 HTML 实体
+            $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
+            // 只保留文本和标点符号(去除所有字母数字以外的特殊符号,可根据需要调整正则)
+            $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Zs}]+/u', '', $content);
+            // 去除多余空白
+            $content = preg_replace('/\s+/u', '', $content);
+            // 截取 100 个字符
+            $data['description'] = mb_substr($content, 0, 100);
+            // 过滤 HTML 标签并去除空格,然后截取前 100 个字符
+        }
       $project = Project::insertGetId($data);
       if(empty($project)){
           return Result::error("暂无相关项目信息", 0);
@@ -6332,9 +6345,23 @@ class NewsService implements NewsServiceInterface
           $data['keyword'] = implode(',', $segList1);
         }
         if ($data['description'] == '') {
-          //提取内容中的描述
-          // var_dump(11111);
-          $data['description'] = mb_substr(str_replace(' ', '', strip_tags($data['introduce'])), 0, 100);
+            //提取内容中的描述
+            // var_dump(11111);
+            //提取内容中的描述
+            $content = $data['introduce'];
+            // 去除 <style> 和 <script> 标签及其内容
+            $content = preg_replace('/<(style|script)[^>]*>.*?<\/\1>/is', '', $content);
+            // 去除所有 HTML 标签
+            $content = strip_tags($content);
+            // 去除 HTML 实体
+            $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
+            // 只保留文本和标点符号(去除所有字母数字以外的特殊符号,可根据需要调整正则)
+            $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Zs}]+/u', '', $content);
+            // 去除多余空白
+            $content = preg_replace('/\s+/u', '', $content);
+            // 截取 100 个字符
+            $data['description'] = mb_substr($content, 0, 100);
+            // 过滤 HTML 标签并去除空格,然后截取前 100 个字符
         }
         $result = Project::where('id', $id)->update($data);
         if(empty($result)){