News Archive
PhpRiot Newsletter
Your Email Address:

More information

How to Create Your Own Twitter Widget in PHP, Part 2

Note: This article was originally published at Planet PHP on 6 January 2011.
Planet PHP

In part 1 of this series, we examined the Twitter API, creating a PHP TwitterStatus class, and imported the latest tweets in JSON format. Today, we'll populate the data into html templates - download the full source code here.

Template parsing

We have defined two html templates as public properties:

  • $WidgetTemplate is the widget's outer html. The {TWEETS} code indicates where $TweetTemplate will appear
  • $TweetTemplate is the html for an individual tweet.

Either template can include {named-values} from the Twitter feed, e.g.

$t-WidgetTemplate = '' . '' . '{name}' '
' . '';$t-TweetTemplate = '
  • Tweet {statuses_count}: {text} {created_at}
  • ';

    Our code must extract all {named-values}, see if a value exists in the Twitter data and replace it accordingly. Let's create a private function to handle that:

    private function ParseStatus($data, $template) {

    ParseStatus accepts two values:

    • $data is a section of the Twitter JSON for an individual tweet.
    • $template is an html template (either $WidgetTemplate or $TweetTemplate will be passed).

    The first step is locate every {named-value} using a regular expression:

    preg_match_all('/{(.+)}/U', $template, $m);

    PHP's preg_match_all() returns an array with two elements. The first is an array containing all matched ao{named-value}a strings. The second is another array containing the characters found within the regular expression's (.+) brackets, i.e. aonamed-valuea without the curly quotes.

    We can loop through each value and determine whether it's in the Twitter data. Note it may reside within the auser' section (a sub-associative array)a

    for ($i = 0, $il = count($m[0]); $i

    At the end of this code $d is either false or a matching tweet value. We could now use it within an str_replace() but there are two special cases we need to handle first:

    • atext' is the tweet string which could contain URLs, @ids or #hashtags. We need to convert those to accordingly, so we'll intercept the data and pass it to a new function named ParseTwitterLinks().
    • acreated_on' is the date/time the tweet was sent. Dates must be handled differently for reasons which will become apparent in the next post. For the moment, we just want to indicate we have a date, so we'll put the value within a {DATE:a} string.
    // replace data if ($d) { switch ($name) { // parse status links case 'text': if ($this-ParseLinks) { $d = $this-ParseTwitterLinks($d); } break; // tweet date case 'created_at': $d = "{DATE:$d}"; break; } $template = str_replace($m[0][$i], $d, $template);} } // end of loop return $template; } // end of ParseStatus

    Link parsing

    We now require a private ParseTwitterLinks() function to translate links in tweets (atext' values). A single string is passed:

    private function ParseTwitterLinks($str) {

    We now need a little more regular expression magic. First, we'll look for URLs - we don't care if they're well-formed, just that they start with ahttp' and contain valid characters:

    $str = preg_replace('/(https{0,1}:\/\/[\w\-\.\/#?&=]*)/', '$1', $str);

    Next, we'll check for @id links and replace them with an appropriate link to Twitter:

    $str = preg_replace('/@(\w+)/', '@$1', $str);

    Finally, we'll replace #hashtags with a Twitter search link:

    $str = preg_replace('/\s#(\w+)/', ' #$1', $str);

    Rendering our html widget

    Truncated by Planet PHP, read more at the original (another 3488 bytes)