我目前正在使用https://github.com/digitalclout/tweetify/blob/master/tweetify.php(粘贴在下面)来创建来自推文的html超链接。我正在尝试添加一个表达式以捕获没有http://前缀的链接,但是我正在努力创建一个不会破坏http://链接的正则表达式,如下所示:
"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>';
}
?>
答案 0 :(得分:0)
请不要编写正则表达式以查找超链接。您需要的所有信息都在Tweet对象中entities。
典型的推文中会有类似的内容
"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]
}],
“索引”会告诉您文本在文本中的确切位置 - 或者您可以使用“expanded_url”来查找它们。