twitter 分享链接_链接您的Twitter Feed

twitter 分享链接

So, you want to display your Twitter status on your blog? No problem, use the API. But, what if you want to display links in your status like twitter.com itself does? No problem, use regular expressions.

那么,您想在博客上显示Twitter状态吗? 没问题,请使用API​​。 但是,如果您想像Twitter.com本身那样以状态显示链接怎么办? 没问题,使用正则表达式。

Here is a function that will turn all HTTP URLs, Twitter @usernames, and #tags into links:

这是一个将所有HTTP URL,Twitter @usernames和#tags转换为链接的函数:

PHP (The PHP)


function linkify_twitter_status($status_text)
{
  // linkify URLs
  $status_text = preg_replace(
    '/(https?:\/\/\S+)/',
    '<a href="\1">\1</a>',
    $status_text
  );

  // linkify twitter users
  $status_text = preg_replace(
    '/(^|\s)@(\w+)/',
    '\1@<a href="http://twitter.com/\2">\2</a>',
    $status_text
  );

  // linkify tags
  $status_text = preg_replace(
    '/(^|\s)#(\w+)/',
    '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>',
    $status_text
  );

  return $status_text;
}



使用它 (Using It)

There are many ways to grab statuses from Twitter. Here, I will show a very simple method with no caching -- suitable for demonstration purposes. Feel free to substitute your favorite Twitter-fetching method.

有很多方法可以从Twitter获取状态。 在这里,我将展示一个没有缓存的非常简单的方法-适用于演示目的。 随意替换您最喜欢的Twitter提取方法。


$url = 'http://twitter.com/statuses/user_timeline/davidwalshblog.json?count=1';
$json = file_get_contents($url);
$statuses = json_decode($json);
$status_text = $statuses[0]->text;

echo "before: ", $status_text, "\n";
echo "after: ", linkify_twitter_status($status_text), "\n";


The above code turns:

上面的代码变成:


@davidwalshblog: Check it out http://is.gd/hd2D #awesome

..into:

@<a href="http://twitter.com/davidwalshblog">davidwalshblog</a>: Check it out <a href="http://is.gd/hd2D">http://is.gd/hd2D</a> #<a href="http://search.twitter.com/search?q=%23awesome">awesome</a>


Pretty simple, eh?

很简单,是吗?

关于杰里米·帕里什(Jeremy Parrish) (About Jeremy Parrish)

Jeremy Parrish is the Systems Administrator for Econoprint, a print/design/web company in Verona, Wisconsin. He has a BS in Computer Engineering and Computer Science from the University of Wisconsin--Madison. In his spare time he enjoys running marathons with his wife and working on various coding projects. You can check out Jeremy's blog at http://rrish.org/.

杰里米·帕里什(Jeremy Parrish)是威斯康星州维罗纳市一家印刷/设计/网络公司Econoprint的系统管理员。 他拥有威斯康星大学麦迪逊分校的计算机工程和计算机科学学士学位。 在业余时间,他喜欢与妻子进行马拉松比赛并从事各种编码项目。 您可以在http://rrish.org/上查看Jeremy的博客。

翻译自: https://davidwalsh.name/linkify-twitter-feed

twitter 分享链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值