Linkify urls on the forum.
This commit is contained in:
parent
42ae2704ae
commit
a924d18be5
2 changed files with 26 additions and 0 deletions
|
@ -39,6 +39,7 @@ class BBCodeParser implements ParserInterface
|
|||
// Links
|
||||
new Tags\NamedUrlTag,
|
||||
new Tags\UrlTag,
|
||||
new Tags\LinkifyTag,
|
||||
|
||||
// Finally parse leftover newlines
|
||||
new Tags\NewLineTag,
|
||||
|
|
25
src/Parsers/BBCode/Tags/LinkifyTag.php
Normal file
25
src/Parsers/BBCode/Tags/LinkifyTag.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
namespace Misuzu\Parsers\BBCode\Tags;
|
||||
|
||||
use Misuzu\Parsers\BBCode\BBCodeTag;
|
||||
|
||||
final class LinkifyTag extends BBCodeTag
|
||||
{
|
||||
public function parseText(string $text): string
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/(^|[\n ])([\w]*?)([\w]*?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is',
|
||||
function ($matches) {
|
||||
$matches[0] = trim($matches[0]);
|
||||
$url = parse_url($matches[0]);
|
||||
|
||||
if (empty($url['scheme']) || !in_array(mb_strtolower($url['scheme']), ['http', 'https', 'ftp'], true)) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
return sprintf('<a href="%1$s" class="link" target="_blank" rel="noreferrer noopener">%1$s</a>', $matches[0]);
|
||||
},
|
||||
$text
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue