Last active
September 17, 2017 08:54
-
-
Save carmichaelize/11077930 to your computer and use it in GitHub Desktop.
Twitter Links, Mentions and Hashtag Regex
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 | |
//Urls | |
$tweet = preg_replace('/(http[^\s]+)/', '<a target="_blank" class="tweet-link" href="$1">$1</a>', $tweet->text); | |
//Mentions | |
$tweet = preg_replace('/\@([a-zA-Z1-9]+)/', '<a title="@$1" target="_blank" href="https://twitter.com/$1" class="tweet-user">@$1</a>', $string); | |
//Hashtags | |
$tweet = preg_replace('/#([a-zA-Z1-9]+)/', '<a title="#$1" target="_blank" href="https://twitter.com/search?q=%23$1&src=hash" class="tweet-hashtag">#$1</a>', $string); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! For mentions and hashtags I replaced
[a-zA-Z1-9]+
with[a-zA-Z0-9_]+
to allow for0
(zero) and_
(underscore).