Last active
March 8, 2022 18:25
-
-
Save guimadaleno/8557811 to your computer and use it in GitHub Desktop.
Quickly add hyperlinks to mentions and hashtags on tweets using PHP
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 | |
function add_twitter_links ($string) | |
{ | |
$string = preg_replace(array | |
( | |
"#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", | |
"#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", | |
"/@(\w+)/", | |
"/#(\w+)/" | |
), array | |
( | |
"\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", | |
"\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", | |
"<a href=\"https://twitter.com/\\1\" target=\"_blank\">@\\1</a>", | |
"<a href=\"https://twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>" | |
), $string); | |
return $string; | |
} | |
echo add_twitter_links ("Heyo @guimadaleno, wazzup? Let's go drink a beer? #dontsayno"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment