function linkify($status, $html = FALSE) { $text = $status->text; if (empty($status->entities)) { $statusId = twitter_id($status); common_log(LOG_WARNING, "No entities data for {$statusId}; trying to fake up links ourselves."); $text = common_replace_urls_callback($text, 'common_linkify'); $text = preg_replace_callback('/(^|\\"\\;|\'|\\(|\\[|\\{|\\s+)#([\\pL\\pN_\\-\\.]{1,64})/', function ($m) { return $m[1] . '#' . TwitterStatusFetcher::tagLink($m[2]); }, $text); $text = preg_replace_callback('/(^|\\s+)@([a-z0-9A-Z_]{1,64})/', function ($m) { return $m[1] . '@' . TwitterStatusFetcher::atLink($m[2]); }, $text); return $text; } // Move all the entities into order so we can // replace them and escape surrounding plaintext // in order $toReplace = array(); if (!empty($status->entities->urls)) { foreach ($status->entities->urls as $url) { $toReplace[$url->indices[0]] = array(self::URL, $url); } } if (!empty($status->entities->hashtags)) { foreach ($status->entities->hashtags as $hashtag) { $toReplace[$hashtag->indices[0]] = array(self::HASHTAG, $hashtag); } } if (!empty($status->entities->user_mentions)) { foreach ($status->entities->user_mentions as $mention) { $toReplace[$mention->indices[0]] = array(self::MENTION, $mention); } } // sort in forward order by key ksort($toReplace); $result = ''; $cursor = 0; foreach ($toReplace as $part) { list($type, $object) = $part; $start = $object->indices[0]; $end = $object->indices[1]; if ($cursor < $start) { // Copy in the preceding plaintext $result .= $this->twitEscape(mb_substr($text, $cursor, $start - $cursor)); $cursor = $start; } $orig = $this->twitEscape(mb_substr($text, $start, $end - $start)); switch ($type) { case self::URL: $linkText = $this->makeUrlLink($object, $orig, $html); break; case self::HASHTAG: if ($html) { $linkText = $this->makeHashtagLink($object, $orig); } else { $linkText = $orig; } break; case self::MENTION: if ($html) { $linkText = $this->makeMentionLink($object, $orig); } else { $linkText = $orig; } break; default: $linkText = $orig; continue; } $result .= $linkText; $cursor = $end; } $last = $this->twitEscape(mb_substr($text, $cursor)); $result .= $last; return $result; }