Twitter text processing library (auto linking and extraction of usernames, lists and hashtags). Based on the Ruby and Java implementations by Matt Sanford
Pull request Compare This branch is 201 commits ahead of ngnpope:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
tests Change default configuration to v2 in Validator Feb 15, 2018

Twitter Text (PHP Edition)

A library of PHP classes that provide auto-linking and extraction of usernames, lists, hashtags and URLs from tweets. Originally created from twitter-text-rb and twitter-text-java projects by Matt Sanford and ported to PHP by Mike Cochrane, this library has been improved and made more complete by Nick Pope.

Build Status Codecov Latest Stable Version


  • PHP 5.3 or higher
  • ext-mbstring
  • ext-intl

If the necessary extensions are not installed on the server, please install it additionally or use symfony/polyfill.


You can install this library into your application using Composer.

composer require nojimage/twitter-text-php



  • Add links to all matching Twitter usernames (no account verification).
  • Add links to all user lists (of the form @username/list-name).
  • Add links to all valid hashtags.
  • Add links to all URLs.
  • Support for international character sets.


  • Extract mentioned Twitter usernames (from anywhere in the tweet).
  • Extract replied to Twitter usernames (from start of the tweet).
  • Extract all user lists (of the form @username/list-name).
  • Extract all valid hashtags.
  • Extract all URLs.
  • Support for international character sets.

Hit Highlighter

  • Highlight text specifed by a range by surrounding with a tag.
  • Support for highlighting when tweet has already been autolinked.
  • Support for international character sets.


  • Validate different twitter text elements.
  • Support for international character sets.


  • Parses a given tweet text with the weighted character count configuration.

Length validation

twitter-text 2.0 introduces configuration files that define how Tweets are parsed for length. This allows for backwards compatibility and flexibility going forward. Old-style traditional 140-character parsing is defined by the v1.json configuration file, whereas v2.json is updated for "weighted" Tweets where ranges of Unicode code points can have independent weights aside from the default weight. The sum of all code points, each weighted appropriately, should not exceed the max weighted length.

Some old methods from twitter-text-php 1.0 have been marked deprecated, such as the Twitter\Text\Validator::isValidTweetText(), Twitter\Text\Validator::getTweetLength() method. The new API is based on the following method, Twitter\Text\Parser::parseTweet()

$result = \Twitter\Text\Parser::parseTweet($text);

This method takes a string as input and returns a results object that contains information about the string. Twitter\Text\ParseResults object includes:

  • weightedLength: the overall length of the tweet with code points weighted per the ranges defined in the configuration file.

  • permillage: indicates the proportion (per thousand) of the weighted length in comparison to the max weighted length. A value > 1000 indicates input text that is longer than the allowable maximum.

  • valid: indicates if input text length corresponds to a valid result.

  • displayRangeStart, displayRangeEnd: An array of two unicode code point indices identifying the inclusive start and exclusive end of the displayable content of the Tweet. For more information, see the description of display_text_range here: Tweet updates

  • validRangeStart, validRangeRnd: An array of two unicode code point indices identifying the inclusive start and exclusive end of the valid content of the Tweet. For more information on the extended Tweet payload see Tweet updates


For examples, please see tests/example.php which you can view in a browser or run from the command line.


You'll need the test data which is in YAML format from the following repository:

twitter/twitter-text already included in composer.json, so you should just need to run:

curl -s | php
php composer.phar install

There are a couple of options for testing conformance:

  • Run phpunit in from the root folder of the project.

Thanks & Contributions

The bulk of this library is from the heroic efforts of: