More Twitter Feed Tweaks

I blogged earlier about how I found this cool PHP class to read my tweets and post them on a marketing blog page. The tweets were just unformatted and didn’t have any links in them so I set about to upgrade the formatting to add links to:

  • @name
  • #keyword
  • http://

Here’s what I did in PHP using the preg_replace function:
foreach($data as $tweet) {
// Replace # with link to, replace @ with link to twitter account
$link = preg_replace(“_http://[a-zA-Z0-9\./]+_”,”<a href=’$0′>$0</a>”,$tweet->text);
$replace_pound = preg_replace(“/#[a-zA-Z0-9]+/”,”<a href=’$0′>$0</a>”,$link);
$html_pound = preg_replace(“/q=#/”,”q=%23″,$replace_pound);
$replace_at = preg_replace(“/@[a-zA-Z0-9_]+/”,”<a href=’$0′>$0</a>”,$html_pound);
$html_at = preg_replace(“”,””,$replace_at);
print “<li>$html_at</li>

Here’s what the linked tweet feed now looks like:

This Post Has One Comment

  1. Daniel Payne

    I created this custom theme from scratch. My business is to develop custom themes, plugins and features for web sites, both WordPress and custom.

Leave a Reply