Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active September 17, 2017 08:54
Show Gist options
  • Save carmichaelize/11077930 to your computer and use it in GitHub Desktop.
Save carmichaelize/11077930 to your computer and use it in GitHub Desktop.
Twitter Links, Mentions and Hashtag Regex
<?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);
@firasd
Copy link

firasd commented Sep 17, 2017

Thanks! For mentions and hashtags I replaced [a-zA-Z1-9]+ with [a-zA-Z0-9_]+ to allow for 0 (zero) and _ (underscore).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment