36
app/Services/TagDetectorService.php
Normal file
36
app/Services/TagDetectorService.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?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
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user