用于从pic.twitter.com/xyz创建URL的PHP表达式
我目前正在使用(粘贴在下面)从tweet创建html超链接。我正试图添加一个表达式来捕获没有http://前缀的链接,但我正在努力创建一个不破坏http://链接的正则表达式,如下所示:用于从pic.twitter.com/xyz创建URL的PHP表达式,php,regex,twitter,Php,Regex,Twitter,我目前正在使用(粘贴在下面)从tweet创建html超链接。我正试图添加一个表达式来捕获没有http://前缀的链接,但我正在努力创建一个不破坏http://链接的正则表达式,如下所示: "shortlink" => '/[a-z0-9-_]+\.[a-z0-9-_@:~%&\?\+#\/.=]+[^:\.,\)\s*$]/i', 电流源: <?php /* * tweetify.php * * Ported from Remy Sharp's 'ify' java
"shortlink" => '/[a-z0-9-_]+\.[a-z0-9-_@:~%&\?\+#\/.=]+[^:\.,\)\s*$]/i',
电流源:
<?php
/*
* tweetify.php
*
* Ported from Remy Sharp's 'ify' javascript function; see:
* http://code.google.com/p/twitterjs/source/browse/trunk/src/ify.js
*
* Based on revision 46:
* http://code.google.com/p/twitterjs/source/detail?spec=svn46&r=46
*
* Forked from https://github.com/fiveminuteargument/tweetify
*/
/*
* Clean a tweet: translate links, usernames beginning '@', and hashtags
*/
function clean_tweet($tweet)
{
$regexps = array
(
"link" => '/[a-z]+:\/\/[a-z0-9-_]+\.[a-z0-9-_@:~%&\?\+#\/.=]+[^:\.,\)\s*$]/i',
"at" => '/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15}(\/[a-zA-Z0-9-_]+)*)/',
"hash" => "/(^|[^&\w'\"]+)\#([a-zA-Z0-9_]+)/"
);
foreach ($regexps as $name => $re)
{
$tweet = preg_replace_callback($re, 'parse_tweet_'.$name, $tweet);
}
return $tweet;
}
/*
* Wrap a link element around URLs matched via preg_replace()
*/
function parse_tweet_link($m)
{
return '<a target="_blank" class="ital_link" href="'.$m[0].'">'.((strlen($m[0]) > 25) ? substr($m[0], 0, 24).'...' : $m[0]).'</a>';
}
/*
* Wrap a link element around usernames matched via preg_replace()
*/
function parse_tweet_at($m)
{
return $m[1].'@<a target="_blank" class="ital_link" href="http://twitter.com/'.$m[2].'">'.$m[2].'</a>';
}
/*
* Wrap a link element around hashtags matched via preg_replace()
*/
function parse_tweet_hash($m)
{
return $m[1].'#<a target="_blank" class="ital_link" href="https://twitter.com/search?q=%23'.$m[2].'">'.$m[2].'</a>';
}
?>
请不要编写正则表达式来查找超链接。您需要的所有信息都在Tweet对象中
一条典型的推文中会有这样的内容
"entities": {
"hashtags": [],
"symbols": [],
"urls": [{
"url": "https:\/\/t.co\/XdXRudPXH5",
"expanded_url": "https:\/\/blog.twitter.com\/2013\/rich-photo-experience-now-in-embedded-tweets-3",
"display_url": "blog.twitter.com\/2013\/rich-phot\u2026",
"indices": [80, 103]
}],
“索引”将告诉您URL在文本中的确切位置,或者您可以使用“扩展URL”查找它们。这不包括推文链接。您是对的。有问题的tweet是为客户端登台而手动添加的,因此我从显示的tweet复制了pic.twitter.com URL。这在API中被转换为一个http://前缀的twitter缩短链接,这否定了我的问题。