Home > Tutorials > PHP > Kohana > Kohana Designers Tip: Twitter API
Permalink to Kohana Designers Tip: Twitter API

Kohana Designers Tip: Twitter API

by on 09.20.2009 | 1 comment

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 [...]

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. :lol:

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. :)

Written by Paul Robinson

A Web coder in languages such as CSS, X/HTML, jQuery, but mostly PHP. Addicted to Girls Aloud, Jennifer Morrison, Carah Faye Charnow, TV Show Chuck, and completely in love with Yvonne Strahovski's smile.

Give something back!

If you LOVED this tutorial and would like to show your appreciation, please consider or a little something from our Amazon Wishlist.

Discussion: 1 Comment

  1. Feb 3rd, 2010 @ 13:04:43

    Howdy that?s a very fascinating view, It does give one food for thought, I am genuinely delighted I stumbled on your blog, i was using Stumbleupon at the time, in any case i don?t want to drift on too much, but i would like to mention that I will be back when I have a little time to read your blog more thoroughly, Once again thank you for the blog post and please do keep up the good work,


Leave a comment

Please enclose code in [lang] tags. For example [php] echo 'hello world'; [/php]

* Name, Email, Comment are Required