Skip to content

Instantly share code, notes, and snippets.

@adactio
Created October 30, 2023 13:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adactio/1c413e1180f8fd5a795275da348f009d to your computer and use it in GitHub Desktop.
Save adactio/1c413e1180f8fd5a795275da348f009d to your computer and use it in GitHub Desktop.
A regular expression to convert @@ usernames into hyperlinks
<?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