Jelajahi Sumber

提取字符串中 {{1-2-3-4}} 格式的内容(四段数字)

visuddhinanda 4 hari lalu
induk
melakukan
b3edb82c52
1 mengubah file dengan 6 tambahan dan 3 penghapusan
  1. 6 3
      api-v12/app/Services/ArticleService.php

+ 6 - 3
api-v12/app/Services/ArticleService.php

@@ -32,14 +32,14 @@ class ArticleService
     }
 
     /**
-     * 提取字符串中 {{ }} 之间的内容
+     * 提取字符串中 {{1-2-3-4}} 格式的内容(四段数字)
      *
      * @param string $text
      * @return array
      */
     public function extractBracesContent(string $text): array
     {
-        preg_match_all('/\{\{\s*(.*?)\s*\}\}/', $text, $matches);
+        preg_match_all('/\{\{\s*(\d+-\d+-\d+-\d+)\s*\}\}/', $text, $matches);
 
         return $matches[1] ?? [];
     }
@@ -66,9 +66,12 @@ class ArticleService
         ];
     }
 
-    public function articleChannels(string $id): array
+    public function articleChannels(string $id): ?array
     {
         $sentences = $this->sentenceIds($id);
+        if (!$sentences) {
+            return null;
+        }
         $query = [];
         foreach ($sentences as $value) {
             $ids = explode('-', $value);