使用带有获取的Twitter状态的preg_replace

时间:2017-02-09 00:28:32

标签: php html twitter-bootstrap twitter preg-replace

  

这里有类似的问题/答案:   Need a good regex to convert URLs to links but leave existing links alone

我已经尝试了几个答案,但没有运气。

我的网站上有一个Twitter Feed轮播。一些推文中有链接。

例如,一条推文是Happy Thanksgiving everyone! https://shortenedURL.com/blahblah

另一个是Full Schnabel Loaded With Wind Tower Base Section Left Turn: https://shortenedURL.com/blahblah via @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);

但没有任何东西在转换。如上所述,我还尝试了上述相关问题中的一些其他示例和答案。

完整代码:

//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.';
}

我尽我所能,不知道还能做什么。

0 个答案:

没有答案