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 itself does? No problem, use regular expressions.
那么,您想在博客上显示Twitter状态吗? 没问题,请使用API。 但是,如果您想像本身那样以状态显示链接怎么办? 没问题,使用正则表达式。
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="/\2">\2</a>',
$status_text
);
// linkify tags
$status_text = preg_replace(
'/(^|\s)#(\w+)/',
'\1#<a href="/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 = '/statuses/user_timeline/?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 /hd2D #awesome
..into:
@<a href="/davidwalshblog">davidwalshblog</a>: Check it out <a href="/hd2D">/hd2D</a> #<a href="/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 /.
杰里米·帕里什(Jeremy Parrish)是威斯康星州维罗纳市一家印刷/设计/网络公司Econoprint的系统管理员。 他拥有威斯康星大学麦迪逊分校的计算机工程和计算机科学学士学位。 在业余时间,他喜欢与妻子进行马拉松比赛并从事各种编码项目。 您可以在/上查看Jeremy的博客。
翻译自: /linkify-twitter-feed
twitter 分享链接