pixelfed/app/Util/Lexer/Hashtag.php

38 wiersze
920 B
PHP
Czysty Zwykły widok Historia

2018-05-20 03:03:17 +00:00
<?php
namespace App\Util\Lexer;
2018-08-28 03:07:36 +00:00
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;
2018-05-20 03:03:17 +00:00
}
2018-08-28 03:07:36 +00:00
public static function replaceHashtagsWithLinks($status)
{
$hashtags = self::getHashtags($status);
if (!$hashtags) {
return false;
}
2018-05-20 03:03:17 +00:00
2018-08-28 03:07:36 +00:00
$rendered = $status;
2018-05-20 03:03:17 +00:00
2018-08-28 03:07:36 +00:00
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);
}
2018-05-20 03:03:17 +00:00
2018-08-28 03:07:36 +00:00
return $rendered;
}
}