forked from mirror/pixelfed
Add hashtag lexer
This commit is contained in:
parent
d9db890514
commit
62da545a94
1 changed files with 36 additions and 0 deletions
36
app/Util/Lexer/Hashtag.php
Normal file
36
app/Util/Lexer/Hashtag.php
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue