ArticleService.php 896 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Article;
  4. class ArticleService
  5. {
  6. public function getRawById(string $id)
  7. {
  8. return Article::find($id);
  9. }
  10. public function getRawByTitle(string $title)
  11. {
  12. $article = Article::where('title', $title)->first();
  13. return $article;
  14. }
  15. public function sentenceIds(string $id): ?array
  16. {
  17. $article = $this->getRawById($id);
  18. if (empty($article->content)) {
  19. return null;
  20. }
  21. $sentenceIds = $this->extractBracesContent($article->content);
  22. return $sentenceIds;
  23. }
  24. /**
  25. * 提取字符串中 {{ }} 之间的内容
  26. *
  27. * @param string $text
  28. * @return array
  29. */
  30. public function extractBracesContent(string $text): array
  31. {
  32. preg_match_all('/\{\{\s*(.*?)\s*\}\}/', $text, $matches);
  33. return $matches[1] ?? [];
  34. }
  35. }