Last active
February 6, 2023 19:30
-
-
Save GreenFootballs/41f3efa9d73fadbc6e8f74a1961198da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mastodonUsers($text) { | |
return preg_replace_callback( | |
'~ | |
(?<= | |
^ | |
| | |
(?<= | |
[^a-zA-Z0-9-.&;/] | |
) | |
) | |
(?(?= | |
<a\b[^>]*>.+?</a>|<[^>]*> | |
) | |
(?: | |
<a\b[^>]*>.+?</a>|<[^>]*> | |
) | |
| | |
(?<name> | |
[@]{1}[A-Za-z_\p{L}]+[A-Za-z0-9_\p{L}]+ | |
) | |
) | |
(?<domain> | |
[@]{1}[A-Z0-9.-]+\.[A-Z]{2,} | |
)\b | |
~uxsi', | |
function($matches) { | |
if (isset($matches['name']) && isset($matches['domain'])) { | |
return '<a href="https://' . $matches['domain'] . '/' . $matches['name'] . '">' . $matches[0] . '</a>'; | |
} else { | |
return $matches[0]; | |
} | |
}, | |
$text | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment