0

There is a similar question/answer here: Need a good regex to convert URLs to links but leave existing links alone

I have tried several of the answers and Haven't had any luck.

I have a Twitter feed carousel on my site. Some of the tweets have links in them.

For example one tweet is Happy Thanksgiving everyone! https://shortenedURL.com/blahblah.

Another is Full Schnabel Loaded With Wind Tower Base Section Left Turn: https://shortenedURL.com/blahblah via @YouTube where @YouTube is already a link(I'm guessing Twitter converted this one.

This is what I tried:

$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);

But nothing is converting. As stated I also tried some of the other examples and answers in the above linked question.

Full code:

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

I've tried whatever I could and don't know what else to do.

Community
  • 1
  • 1
Jesse Elser
  • 974
  • 2
  • 11
  • 39

0 Answers0