Created
October 30, 2023 13:11
-
-
Save adactio/1c413e1180f8fd5a795275da348f009d to your computer and use it in GitHub Desktop.
A regular expression to convert @@ usernames into hyperlinks
This file contains 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 | |
/* Pass a string of text into the function to convert @@ usernames. */ | |
function autoLinkMastodonUsernames($string) { | |
return preg_replace( | |
'/@?\b([A-Za-zŽžÀ-ÿ0-9._%+-]+)@([A-Za-zŽžÀ-ÿ0-9.-]+\.[A-Za-zŽžÀ-ÿ]{2,})\b/i', | |
'<a href="https://$2/@$1">$0</a>', | |
$string | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment