Browse Source

Add hashtag lexer

Daniel Supernault 7 years ago
parent
commit
62da545a94
1 changed files with 36 additions and 0 deletions
  1. 36 0
      app/Util/Lexer/Hashtag.php

+ 36 - 0
app/Util/Lexer/Hashtag.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace App\Util\Lexer;
+
+class Hashtag {
+  
+  public static function getHashtags($status)
+  {  
+    $hashtags = false;  
+    preg_match_all("/(#\w+)/u", $status, $matches);
+    if ($matches) {
+        $res = array_count_values($matches[0]);
+        $hashtags = array_keys($res);
+    }
+    return $hashtags;
+  }
+
+  public static function replaceHashtagsWithLinks($status)
+  {
+    $hashtags = self::getHashtags($status);
+    if(!$hashtags) { return false; }
+
+    $rendered = $status;
+
+    foreach($hashtags as $hashtag) {
+      $tag = substr($hashtag, 1);
+      $link = config('routes.hashtag.search') . $tag;
+      $href = "<a href='{$link}' class='mention hashtag status-link' rel='noopener'>{$hashtag}</a>";
+      $rendered = str_replace($hashtag, $href, $rendered);
+    }
+
+    return $rendered;
+
+  }
+
+}