37 lines
744 B
PHP
37 lines
744 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class TagDetectorService
|
|
{
|
|
public const REGEX_FIND = '/\s#([^\s]+)(?:\s|$)/m';
|
|
public const REGEX_REPLACE = '/\s(#[^\s]+)(?:\s|$)/m';
|
|
|
|
/**
|
|
* @param string $text
|
|
*
|
|
* @return array
|
|
*/
|
|
public function detectFrom($text)
|
|
{
|
|
if (preg_match_all(self::REGEX_FIND, $text, $tags) !== false) {
|
|
return $tags[1];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @param string $text
|
|
*
|
|
* @return string|string[]|null
|
|
*/
|
|
public function linkTagFrom($text)
|
|
{
|
|
return preg_replace(
|
|
self::REGEX_FIND, str_replace('$1', '$1', sprintf(' <a href="%s/%s">%s</a> ', url('stats'), '$1', '#$1')),
|
|
$text
|
|
);
|
|
}
|
|
}
|