/** * Reads in a tweet to be parsed and validates it. * * @param string $tweet The tweet to validate. */ public function __construct($tweet = null, $config = null) { parent::__construct($tweet); if (!empty($config)) { $this->setConfiguration($config); } $this->extractor = Twitter_Extractor::create(); }
function extractIt($string) { require_once 'library/extractor/Extractor.php'; $extractor = Twitter_Extractor::create(); return $extractor->extract($string); }
echo PHP_EOL; } $pass_total = 0; $fail_total = 0; $pass_group = 0; $fail_group = 0; output_preamble(); output_h1('Twitter Text (PHP Edition) Library » Conformance'); output_h2('Extraction Conformance'); # timer $timerStart = microtime(true); # Load the test data. $data = Yaml::parse($DATA . '/extract.yml'); # Define the functions to be tested. $functions = array('hashtags' => 'extractHashtags', 'cashtags' => 'extractCashtags', 'urls' => 'extractURLs', 'mentions' => 'extractMentionedScreennames', 'replies' => 'extractReplyScreenname', 'hashtags_with_indices' => 'extractHashtagsWithIndices', 'cashtags_with_indices' => 'extractCashtagsWithIndices', 'urls_with_indices' => 'extractURLsWithIndices', 'mentions_with_indices' => 'extractMentionedScreennamesWithIndices', 'mentions_or_lists_with_indices' => 'extractMentionsOrListsWithIndices'); $extractor = Twitter_Extractor::create(); # Perform testing. foreach ($data['tests'] as $group => $tests) { output_h3('Test Group - ' . ucfirst(str_replace('_', ' ', $group))); if (!array_key_exists($group, $functions)) { output_skip_test(); continue; } $function = $functions[$group]; $pass_group = 0; $fail_group = 0; if ($browser) { echo '<ul>', PHP_EOL; } foreach ($tests as $test) { echo $browser ? '<li>' : ' - ';
/** * Reads in a tweet to be parsed and converted to contain links. * * As the intent is to produce links and output the modified tweet to the * user, we take this opportunity to ensure that we escape user input. * * @see htmlspecialchars() * * @param string $tweet The tweet to be converted. * @param bool $escape Whether to escape the tweet (default: true). * @param bool $full_encode Whether to encode all special characters. */ public function __construct($tweet = null, $escape = true, $full_encode = false) { if ($escape && !empty($tweet)) { if ($full_encode) { parent::__construct(htmlentities($tweet, ENT_QUOTES, 'UTF-8', false)); } else { parent::__construct(htmlspecialchars($tweet, ENT_QUOTES, 'UTF-8', false)); } } else { parent::__construct($tweet); } $this->extractor = Twitter_Extractor::create(); }
/** * Check whether a hashtag is valid. * * @return boolean Whether the hashtag is valid. */ public function validateHashtag() { $length = mb_strlen($this->tweet); if (!$this->tweet || !$length) { return false; } $extracted = Twitter_Extractor::create($this->tweet)->extractHashtags(); return count($extracted) === 1 && $extracted[0] === substr($this->tweet, 1); }
\$tweet = 'Tweet mentioning @mikenz and referring to his list @mikeNZ/sports and website http://mikenz.geek.nz #awesome'; \$data = Twitter_Extractor::create(\$tweet) ->extract(); print_r(\$data); EOPHP; if ($browser) { echo '<h3>Source</h3>', PHP_EOL; echo '<pre class="source">'; highlight_string($code); echo '</pre>', PHP_EOL; } else { echo 'Source:', PHP_EOL, PHP_EOL; echo $code; echo PHP_EOL, PHP_EOL; } $data = Twitter_Extractor::create($tweet)->extract(); if ($browser) { echo '<h3>Output</h3>', PHP_EOL; echo '<pre class="output">'; print_array($data); echo '</pre>', PHP_EOL; } else { echo 'Output:', PHP_EOL, PHP_EOL; print_array($data); echo PHP_EOL, PHP_EOL; } if ($browser) { echo '<h2>'; } echo 'Autolink Examples'; if ($browser) {
function twitter_is_reply($status) { if (!user_is_authenticated()) { return false; } $user = user_current_username(); // Use Twitter Entities to see if this contains a mention of the user if ($status->entities) { if ($status->entities->user_mentions) { $entities = $status->entities; foreach ($entities->user_mentions as $mentions) { if ($mentions->screen_name == $user) { return true; } } } return false; } // If there are no entities (for example on a search) do a simple regex $found = Twitter_Extractor::create($status->text)->extractMentionedUsernames(); foreach ($found as $mentions) { // Case insensitive compare if (strcasecmp($mentions, $user) == 0) { return true; } } return false; }
echo $browser ? '<p>' : " [1;35m"; echo 'Skipping Test...'; echo $browser ? '</p>' : "[0m" . PHP_EOL; echo PHP_EOL; continue; } $function = $functions[$group]; $pass_group = 0; $fail_group = 0; if ($browser) { echo '<ul>', PHP_EOL; } foreach ($tests as $test) { echo $browser ? '<li>' : ' - '; echo $test['description'], ' ... '; $extracted = Twitter_Extractor::create($test['text'])->{$function}(); if ($test['expected'] == $extracted) { $pass_group++; echo $browser ? '<span class="pass">PASS</span>' : "[1;32mPASS[0m"; } else { $fail_group++; echo $browser ? '<span class="fail">FAIL</span>' : "[1;31mFAIL[0m"; if ($browser) { echo '<pre>'; echo 'Original: ' . htmlspecialchars($test['text'], ENT_QUOTES, 'UTF-8', false), PHP_EOL; echo 'Expected: ' . pretty_format($test['expected']), PHP_EOL; echo 'Actual: ' . pretty_format($extracted); echo '</pre>'; } else { echo PHP_EOL, PHP_EOL; echo ' Original: ' . $test['text'], PHP_EOL;
function twitter_parse_tags($input, $entities = false, $id = false, $source = NULL) { // Filter. if ($id && substr($_GET["q"], 0, 6) !== "status" && setting_fetch('filtero', 'no') == 'yes' && twitter_timeline_filter($input . ' ' . $source)) { return "<a href='" . BASE_URL . "status/{$id}' style='text-decoration:none;'><small>[" . __("Tweet Filtered") . "]</small></a>"; } // Linebreaks. Some clients insert \n for formatting. $out = nl2br($input); // Use the Entities to replace hyperlink URLs if ($entities && $entities->urls) { foreach ($entities->urls as $urls) { if ($urls->expanded_url != "") { $display_url = $urls->expanded_url; } else { $display_url = $urls->url; } $url_detect = parse_url($display_url); if (isset($url_detect["scheme"])) { $link_html = theme('external_link', $display_url); $url = $urls->url; // Replace all URLs *UNLESS* they have already been linked (for example to an image) $pattern = '#((?<!href\\=(\'|\\"))' . preg_quote($url, '#') . ')#i'; $out = preg_replace($pattern, $link_html, $out); } } } else { // If Entities haven't been returned, use Autolink // Create an array containing all URLs $urls = Twitter_Extractor::create($input)->extractURLs(); // Hyperlink the URLs $out = Twitter_Autolink::create($out)->addLinksToURLs(); // Hyperlink the # $out = Twitter_Autolink::create($out)->setTarget('')->addLinksToHashtags(); } // Hyperlink the @ and lists $out = Twitter_Autolink::create($out)->setTarget('')->addLinksToUsernamesAndLists(); // Hyperlink the # $out = Twitter_Autolink::create($out)->setTarget('')->addLinksToHashtags(); //Return the completed string return $out; }
/** * linkify text * * @param string $value * @param array $options * username: linkify username. eg. @username * hashtag : linkify hashtag. eg. #hashtag * url : linkify url. eg. http://example.com/ * @return string */ public function linkify($value, $options = array()) { $default = array('url' => true, 'username' => true, 'hashtag' => true); $options = am($default, $options); $extractor = Twitter_Extractor::create($value); $linker = TwimAutolink::create($value, $options); $entities = array(); // autolink if ($options['url']) { $entities = am($entities, $extractor->extractURLWithoutProtocol(false)->extractURLsWithIndices()); } if ($options['hashtag']) { $entities = am($entities, $extractor->extractHashtagsWithIndices()); } if ($options['username']) { $entities = am($entities, $extractor->extractMentionsOrListsWithIndices()); } $entities = $extractor->removeOverlappingEntities($entities); $tweet = $linker->autoLinkEntities($value, $entities); return $tweet; }
/** * Determines the length of a tweet. Takes shortening of URLs into account. * * @return int the length of a tweet. */ public function getLength() { $length = mb_strlen($this->tweet); $urls_with_indices = Twitter_Extractor::create($this->tweet)->extractURLsWithIndices(); foreach ($urls_with_indices as $x) { $length += $x['indices'][0] - $x['indices'][1]; $length += stripos($x['url'], 'https://') === 0 ? self::SHORT_URL_LENGTH_HTTPS : self::SHORT_URL_LENGTH; } return $length; }
/** * @dataProvider extractMentionedUsernamesWithIndicesProvider */ public function testExtractMentionedUsernamesWithIndices($description, $text, $expected) { $extracted = Twitter_Extractor::create($text)->extractMentionedUsernamesWithIndices(); $this->assertSame($expected, $extracted, $description); }
public function saving($model) { $hashtags = Twitter_Extractor::create($model->title)->extractHashtags(); $model->tag($hashtags); }
public function testExtractURLsWithIndicesWithoutProtocol() { $extracted = Twitter_Extractor::create('text: example.com')->extractUrlWithoutProtocol(false)->extractURLsWithIndices(); $this->assertSame(array(), $extracted, 'Unextract url without protocol'); }