visuddhinanda 1 неделя назад
Родитель
Сommit
ce2803d9ca
1 измененных файлов с 22 добавлено и 0 удалено
  1. 22 0
      api-v12/app/Services/ArticleService.php

+ 22 - 0
api-v12/app/Services/ArticleService.php

@@ -15,4 +15,26 @@ class ArticleService
         $article = Article::where('title', $title)->first();
         $article = Article::where('title', $title)->first();
         return $article;
         return $article;
     }
     }
+    public function sentenceIds(string $id): ?array
+    {
+        $article = $this->getRawById($id);
+        if (empty($article->content)) {
+            return null;
+        }
+        $sentenceIds = $this->extractBracesContent($article->content);
+        return $sentenceIds;
+    }
+
+    /**
+     * 提取字符串中 {{ }} 之间的内容
+     *
+     * @param string $text
+     * @return array
+     */
+    public function extractBracesContent(string $text): array
+    {
+        preg_match_all('/\{\{\s*(.*?)\s*\}\}/', $text, $matches);
+
+        return $matches[1] ?? [];
+    }
 }
 }