You are here

public static function TwitterWidgetFormatter::parseTwitterLinks in Twitter Profile Widget 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php \Drupal\twitter_profile_widget\Plugin\Field\FieldFormatter\TwitterWidgetFormatter::parseTwitterLinks()

Helper function.

Parses tweet text and replaces links, at-mentions, and hashtags.

Parameters

string $text: String with the tweet.

Return value

string Converted tweet that has anchor links to Twitter entity types.

1 call to TwitterWidgetFormatter::parseTwitterLinks()
TwitterWidgetFormatter::prepareTweets in src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php
Helper to parse Twitter's JSON and return a normalized array of tweets.

File

src/Plugin/Field/FieldFormatter/TwitterWidgetFormatter.php, line 166

Class

TwitterWidgetFormatter
Plugin implementation of the 'twitter_widget' formatter.

Namespace

Drupal\twitter_profile_widget\Plugin\Field\FieldFormatter

Code

public static function parseTwitterLinks($text) {

  // Links.
  $text = preg_replace('@(https?://([-\\w\\.]+)+(/([\\w/_\\.]*(\\?\\S+)?(#\\S+)?)?)?)@', '<a href="$1">$1</a>', $text);

  // @ mentions.
  $text = preg_replace('/@(\\w+)/', '<a href="//twitter.com/$1">@$1</a>', $text);

  // Hashtags.
  $text = preg_replace('/\\s#(\\w+)/', ' <a href="//twitter.com/search?q=%23$1">#$1</a>', $text);
  return $text;
}