Add hashtag lexer

This commit is contained in:
Daniel Supernault 2018-05-19 21:03:17 -06:00
parent d9db890514
commit 62da545a94
1 changed files with 36 additions and 0 deletions

View File

@ -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;
}
}