Php 使用preg_替换为获取的Twitter状态
这里有一个类似的问题/答案: 我已经尝试了几个答案,但没有任何运气 我的网站上有一个推特提要转盘。一些推文中有链接 例如,一条推特是Php 使用preg_替换为获取的Twitter状态,php,html,twitter-bootstrap,twitter,preg-replace,Php,Html,Twitter Bootstrap,Twitter,Preg Replace,这里有一个类似的问题/答案: 我已经尝试了几个答案,但没有任何运气 我的网站上有一个推特提要转盘。一些推文中有链接 例如,一条推特是祝大家感恩节快乐!https://shortenedURL.com/blahblah 另一个是满载风塔底座的全Schnabel段左转:https://shortenedURL.com/blahblah 通过@YouTube,其中@YouTube已经是一个链接(我猜Twitter转换了这个链接) 这就是我所尝试的: $tweetText = $tweet->t
祝大家感恩节快乐!https://shortenedURL.com/blahblah
另一个是满载风塔底座的全Schnabel段左转:https://shortenedURL.com/blahblah 通过@YouTube
,其中@YouTube
已经是一个链接(我猜Twitter转换了这个链接)
这就是我所尝试的:
$tweetText = $tweet->text;
$tweetText = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i','<a href="\0" target="_blank">\0</a>', $tweetText);
$tweetText=$tweet->text;
$tweetText=preg_replace(“@(?!(!!!!?”,$tweetText);
但是没有任何东西可以转化。正如前面所说的,我也尝试了上面链接的问题中的其他一些例子和答案
完整代码:
//Path to TwitterOAuth library
require_once("twitter/twitteroauth.php");
//Configuration
$twitterID = (isset($_REQUEST['twitterUsername']) && !empty($_REQUEST['twitterUsername']))?$_REQUEST['twitterUsername']:"username";
$tweetNum = 10;
$consumerKey = "consumerkey";
$consumerSecret = "consumersecret";
$accessToken = "accesstoken";
$accessTokenSecret = "accesstokensecret";
if($twitterID && $consumerKey && $consumerSecret && $accessToken && $accessTokenSecret) {
//Authentication with twitter
$twitterConnection = new TwitterOAuth(
$consumerKey,
$consumerSecret,
$accessToken,
$accessTokenSecret
);
//Get user timeline feeds
$twitterData = $twitterConnection->get(
'statuses/user_timeline',
array(
'screen_name' => $twitterID,
'count' => $tweetNum,
'exclude_replies' => true
)
);
?>
<div class='row row-dark'>
<div class='col-md-offset-2 col-md-8'>
<div class="carousel slide" data-ride="carousel" data-interval="4000" data-pause="true" id="quote-carousel">
<div class="carousel-inner">
<?php
if(!empty($twitterData)) {
$counter = 0;
foreach($twitterData as $tweet):
$latestTweet = $tweet->text;
$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
$latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a class="tweet-author" href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
$tweetTime = date("l F d, Y h:i:s A",strtotime($tweet->created_at));
$twitterProfileImg = $tweet->user->profile_image_url;
$twitterProfileImg = str_replace("_normal","",$twitterProfileImg);
$tweetText = $tweet->text;
$tweetText = preg_replace('@(?!(?!.*?<a)[^<]*<\/a>)(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&#/%=~_|$?!:,.]*[A-Z0-9+&#/%=~_|$]@i','<a href="\0" target="_blank">\0</a>', $tweetText);
?>
<div class="item <?php if($counter == 1) { echo 'active'; } ?>">
<blockquote>
<div class="row">
<div class="col-sm-3 text-center">
<img class="img-circle" src="<?php echo $twitterProfileImg; ?>" style="width: 100px;height:100px;">
</div>
<div class="col-sm-9">
<p title="<?php echo $tweetText; ?>"><?php echo $latestTweet; ?></p>
<small>Date: <?php echo $tweetTime; ?> Favorite: <?php echo $tweet->favorite_count; ?> <br><a href="<?php echo $tweet->user->url; ?>">Visit <?php echo $tweet->user->name; ?> On Twitter</a></small>
</div>
</div>
</blockquote>
</div>
<?php
$counter++;
endforeach;
}else{
echo '<li class="tweet-wrapper">Tweets not found for the given username.</li>';
}
?>
</div>
<a data-slide="prev" href="#quote-carousel" class="left carousel-control icon-prev"><i class="fa fa-chevron-left"></i></a>
<a data-slide="next" href="#quote-carousel" class="right carousel-control icon-next"><i class="fa fa-chevron-right"></i></a>
</div>
</div>
</div>
<?php
}else{
echo 'Authentication failed, please try again.';
}
//TwitterOAuth库的路径
需要一次(“twitter/twitterouth.php”);
//配置
$twitterID=(isset($_请求['twitterUsername'])和&!empty($_请求['twitterUsername'])?$_请求['twitterUsername']:“用户名”;
$tweetNum=10;
$consumerKey=“consumerKey”;
$consumerSecret=“consumerSecret”;
$accessToken=“accessToken”;
$accessTokenSecret=“accessTokenSecret”;
if($twitterID&&$consumerKey&&$ConsumerCret&&&$accessToken&&&$accessTokenSecret){
//使用twitter进行身份验证
$twitterConnection=新建TwitterOAuth(
$consumerKey,
$ConsumerCret,
$accessToken,
$accessTokenSecret
);
//获取用户时间线提要
$twitterData=$twitterConnection->get(
“状态/用户时间线”,
排列(
“screen\u name”=>$twitterID,
“count”=>$tweetNum,
“排除答复”=>true
)
);
?>
为什么要使用正则表达式而不是提供的Twitter实体?这些实体拥有替换链接所需的所有信息。这些实体是什么意思?