Ok so I personally am not much of a designer, to be frankly honest I’m rubbish at it. So when it comes to designing things I turn to Lisa Marie of lisa-marieart.com, mainly because she is my sister, but also because she’s an amazing designer. Anyway she is currently redesigning her website using Kohana & decided she wants her twitter feed on her site, so I found a Twitter API module available on Kohana’s project pages here.
Hash Tag & @reply Colorising
Being a designer & the Twitter API module being excellent but a no frills solution she wanted to add some designing flair to it, like for example a different text color to #tags & @replies. She also wanted URLs to be auto hyperlinked, but that is easily accomplished with Kohana’s
text::auto_link_urls(). So here comes the tip, just do the following to colorise the hash tags & at replies:
//Get tweets by making new instance of twitter module & returning our user timeline
$twitter = new Twitter($username, $password);
$mytweets = $twitter->user_timeline($username, 10);
//Autolink any URLs
$mytweets = text::auto_link_urls($mytweets);
//Color the @replies
$mytweets = preg_replace('~(\@[a-z0-9]+)~is', '<span class="atreplies">$1</span>', $mytweets);
//Color the #tags
$mytweets = preg_replace('~(\#[a-z0-9]+)~is', '<span class="hashtags">$1</span>', $mytweets);
//do whatever with the end $mytweets variable
But What Does It Mean?!
Here is what is going on with the
preg_replace(). We use parentheses around the regex to make it a group as we need the info back in the replace. We look for an @ (backslashing it as it is a special char in regex) then any combination & any amount of the characters ‘a’ through ‘z’ & ‘0’ through ‘9’. The ‘i’ & ‘s’ at the end mean case-insensitive & ignore new line characters respectively.
There we have it. There is probably a better way to do it such as combining the two, however I haven’t been working with regex for long as it isn’t my fave language so I’m quite proud of it… I know sad isn’t it. 😆
If you have a neater/nicer way of doing this or you have found an error please let me know, it helps me keep improving my code & in the end allows me to give you better tutorials. 🙂