Răsfoiți Sursa

add contains

sark 1 lună în urmă
părinte
comite
41a45642b9
2 a modificat fișierele cu 74 adăugiri și 0 ștergeri
  1. 5 0
      src/Pages/page.php
  2. 69 0
      src/helpers.php

+ 5 - 0
src/Pages/page.php

@@ -40,6 +40,10 @@ $pageContent = $currentPage['content'] ?? '';
 // Получаем топ контент если есть
 $topContent = $currentPage['top_content'] ?? '';
 
+// Подготавливаем содержание для контента
+$headings = getContainsByText($pageContent);
+$pageContent = addIdToHInText($pageContent, $headings);
+
 // Получаем FAQ для этой страницы
 $faqItems = $faqModel->getByMorphable('page', $currentPage['id']);
 
@@ -69,6 +73,7 @@ return ViewRender($layoutFile, [
     'pageContent' => $pageContent,
     'pageData' => $currentPage,
     'topContent' => $topContent,
+    'headings' => $headings,
     'currentDomain' => $currentDomain,
     'currentUrl' => $currentUrl,
     'isHomepage' => !empty($currentPage['is_homepage']),

+ 69 - 0
src/helpers.php

@@ -32,6 +32,75 @@ class Score
         return self::$tmpArray;
     }
 }
+function getContainsByText(?string $text): array
+{
+    $h2Array = [];
+    $h3 = [];
+    $activeId = '';
+
+    preg_match_all('/<h2[^>]*>(.*?)<\/h2>|<h3[^>]*>(.*?)<\/h3>/i', $text, $matches);
+
+    if (empty($matches[0])) {
+        return [];
+    }
+
+    foreach ($matches[1] as $key => $textH2) {
+        if (empty($textH2) && isset($matches[2][$key]) && ! empty($matches[2][$key])) {
+            $h3[] = [
+                'id' => Str::slug($matches[2][$key]),
+                'text' => $matches[2][$key],
+            ];
+
+            continue;
+        }
+
+        if (! empty($h3)) {
+            $h2Array[$activeId]['h3'] = $h3;
+        }
+
+        $h3 = [];
+        $activeText = $textH2;
+        $activeId = Str::slug($textH2);
+        $h2Array[$activeId] = [
+            'id' => $activeId,
+            'text' => $activeText,
+            'h3' => $h3,
+        ];
+    }
+
+    if (! empty($h3)) {
+        $h2Array[$activeId]['h3'] = $h3;
+    }
+
+    return $h2Array;
+}
+
+function addIdToHInText(?string $text, array $h2AnchorsArray = []): ?string
+{
+    if (empty($h2AnchorsArray)) {
+        $h2AnchorsArray = getContainsByText($text);
+    }
+
+    foreach ($h2AnchorsArray as $data) {
+        if (empty($data['text'])) {
+            continue;
+        }
+
+        $h2Pattern = '/<h2([^>]*)>'.preg_quote($data['text'], '/').'<\/h2>/i';
+        $replacementH2 = "<h2\$1 id=\"{$data['id']}\">".str_replace('$', '\\$', $data['text']).'</h2>';
+        $text = preg_replace($h2Pattern, $replacementH2, $text);
+
+        if (! empty($data['h3'])) {
+            foreach ($data['h3'] as $h3data) {
+                $h3Pattern = '/<h3([^>]*)>'.preg_quote($h3data['text'], '/').'<\/h3>/i';
+                $replacementH3 = "<h3\$1 id=\"{$h3data['id']}\">".str_replace('$', '\\$', $h3data['text']).'</h3>';
+                $text = preg_replace($h3Pattern, $replacementH3, $text);
+            }
+        }
+    }
+
+    return $text;
+}
 
 function array_to_csv_download($array, $filename = "export.csv", $delimiter = ";") {
     $maxArray = [];