Joel Glovier
Joel Glovier

Reputation: 7679

How can I make links out of my PHP twitter feed output?

I'm using a very simple PHP include to grab my current twitter status and output the content into my website. The trouble though, is that the output doesn't generate links like the actual status does when there are URLs in the status.

What is the best or simplest way to make this happen?

Here's the PHP I'm using to populate the status:

<?php  
$response = new SimpleXMLElement('http://twitter.com/users/show/jglovier.xml',NULL,TRUE);  
echo $response->status->text.'';  
?>

*EDIT: since I'm only a front-end developer most comfortable with HTML, CSS, and barely JS, I'm looking for the simplest fix here, preferably JS based.

Upvotes: 1

Views: 793

Answers (3)

GT22
GT22

Reputation: 503

I presume you've already solved this as it's been a while since it was posted, but for users wanting to do something similar, I've found this very useful: http://remysharp.com/2007/05/18/add-twitter-to-your-blog-step-by-step/

Brings in your tweets (as many or few as you specify), but the best thing is, you write out the template of how the tweet is displayed - so time/tweet/poster or poster/tweet/time etc.

Very cool!

Upvotes: 0

abraham
abraham

Reputation: 47833

Try using bcherry/twitter-text-js. It will autolink URLs, @mentions, and #hashtags to the specification that twitter.com uses.

Put twitter-text.js on your server, load it after you load jQuery, and run the snippet below on document ready.

$('p.status').each(function(index, element) {
  $(element).html(twttr.txt.autoLink($(element).text()));
});

Upvotes: 4

CaseySoftware
CaseySoftware

Reputation: 3125

It's actually a really easy few lines of code:

function twitterify($ret) {
    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t\\2", $ret);
    $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r\\2", $ret);
    $ret = preg_replace("/@(\w+)/", "@\\1", $ret);
    $ret = preg_replace("/#(\w+)/", "#\\1", $ret);
    return $ret;
}

Source: http://www.snipe.net/2009/09/php-twitter-clickable-links

Upvotes: 1

Related Questions