PHP Class Twitter_Autolink, twitter-text-php

Parses tweets and generates HTML anchor tags around URLs, usernames, username/list pairs and hashtags. Originally written by {@link http://github.com/mikenz Mike Cochrane}, this is based on code by {@link http://github.com/mzsanford Matt Sanford} and heavily modified by {@link http://github.com/ngnpope Nick Pope}.
Author: Mike Cochrane ([email protected])
Author: Nick Pope ([email protected])
Inheritance: extends Twitter_Regex
显示文件 Open project: ngnpope/twitter-text-php Class Usage Examples

Protected Properties

Property Type Description
$class_cash string CSS class for auto-linked cashtag URLs.
$class_hash string CSS class for auto-linked hashtag URLs.
$class_list string CSS class for auto-linked list URLs.
$class_url string CSS class for auto-linked URLs.
$class_user string CSS class for auto-linked username URLs.
$external boolean Often this is used to be matched on in JavaScript for dynamically adding the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has been undeprecated and thus the 'target' attribute can be used. If this is set to false then the 'target' attribute will be output.
$nofollow boolean Whether to include the value 'nofollow' in the 'rel' attribute.
$target string Support for the 'target' attribute was deprecated in HTML 4.01 but has since been reinstated in HTML 5. To output the 'target' attribute you must disable the adding of the string 'external' to the 'rel' attribute.
$url_base_cash string URL base for cashtag links (the hashtag without the $ will be appended).
$url_base_hash string URL base for hashtag links (the hashtag without the # will be appended).
$url_base_list string URL base for list links (the username/list without the @ will be appended).
$url_base_user string URL base for username links (the username without the @ will be appended).

Public Methods

Method Description
__construct ( string $tweet, boolean $escape = true, boolean $full_encode = false ) Reads in a tweet to be parsed and converted to contain links.
addLinks ( ) : string Adds links to all elements in the tweet.
addLinksToCashtags ( ) : string Adds links to cashtag elements in the tweet.
addLinksToHashtags ( ) : string Adds links to hashtag elements in the tweet.
addLinksToURLs ( ) : string Adds links to URL elements in the tweet.
addLinksToUsernamesAndLists ( ) : string Adds links to username/list elements in the tweet.
create ( string $tweet, boolean $full_encode = false ) : Twitter_Autolink Provides fluent method chaining.
getCashtagClass ( ) : string CSS class for auto-linked cashtag URLs.
getExternal ( ) : boolean Whether to include the value 'external' in the 'rel' attribute.
getHashtagClass ( ) : string CSS class for auto-linked hashtag URLs.
getListClass ( ) : string CSS class for auto-linked username/list URLs.
getNoFollow ( ) : boolean Whether to include the value 'nofollow' in the 'rel' attribute.
getTarget ( ) : string The scope to open the link in.
getURLClass ( ) : string CSS class for auto-linked URLs.
getUsernameClass ( ) : string CSS class for auto-linked username URLs.
setCashtagClass ( string $v ) : Twitter_Autolink CSS class for auto-linked cashtag URLs.
setExternal ( boolean $v ) : Twitter_Autolink Whether to include the value 'external' in the 'rel' attribute.
setHashtagClass ( string $v ) : Twitter_Autolink CSS class for auto-linked hashtag URLs.
setListClass ( string $v ) : Twitter_Autolink CSS class for auto-linked username/list URLs.
setNoFollow ( boolean $v ) : Twitter_Autolink Whether to include the value 'nofollow' in the 'rel' attribute.
setTarget ( string $v ) : Twitter_Autolink The scope to open the link in.
setURLClass ( string $v ) : Twitter_Autolink CSS class for auto-linked URLs.
setUsernameClass ( string $v ) : Twitter_Autolink CSS class for auto-linked username URLs.

Protected Methods

Method Description
_addLinksToCashtags ( array $matches ) : string Callback used by the method that adds links to cashtags.
_addLinksToHashtags ( array $matches ) : string Callback used by the method that adds links to hashtags.
_addLinksToURLs ( array $matches ) : string Callback used by the method that adds links to URLs.
_addLinksToUsernamesAndLists ( array $matches ) : string Callback used by the method that adds links to username/list pairs.
wrap ( string $url, string $class, string $element ) : string Wraps a tweet element in an HTML anchor tag using the provided URL.
wrapHash ( string $url, string $class, string $element ) : string Wraps a tweet element in an HTML anchor tag using the provided URL.

Method Details

__construct() public method

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 also: htmlspecialchars()
public __construct ( string $tweet, boolean $escape = true, boolean $full_encode = false )
$tweet string The tweet to be converted.
$escape boolean Whether to escape the tweet (default: true).
$full_encode boolean Whether to encode all special characters.

_addLinksToCashtags() protected method

Callback used by the method that adds links to cashtags.
See also: addLinksToCashtags()
protected _addLinksToCashtags ( array $matches ) : string
$matches array The regular expression matches.
return string The link-wrapped cashtag.

_addLinksToHashtags() protected method

Callback used by the method that adds links to hashtags.
See also: addLinksToHashtags()
protected _addLinksToHashtags ( array $matches ) : string
$matches array The regular expression matches.
return string The link-wrapped hashtag.

_addLinksToURLs() protected method

Callback used by the method that adds links to URLs.
See also: addLinksToURLs()
protected _addLinksToURLs ( array $matches ) : string
$matches array The regular expression matches.
return string The link-wrapped URL.

_addLinksToUsernamesAndLists() protected method

Callback used by the method that adds links to username/list pairs.
See also: addLinksToUsernamesAndLists()
protected _addLinksToUsernamesAndLists ( array $matches ) : string
$matches array The regular expression matches.
return string The link-wrapped username/list pair.

addLinksToCashtags() public method

Adds links to cashtag elements in the tweet.
public addLinksToCashtags ( ) : string
return string The modified tweet.

addLinksToHashtags() public method

Adds links to hashtag elements in the tweet.
public addLinksToHashtags ( ) : string
return string The modified tweet.

addLinksToURLs() public method

Adds links to URL elements in the tweet.
public addLinksToURLs ( ) : string
return string The modified tweet.

addLinksToUsernamesAndLists() public method

Adds links to username/list elements in the tweet.
public addLinksToUsernamesAndLists ( ) : string
return string The modified tweet.

create() public static method

Provides fluent method chaining.
See also: __construct()
public static create ( string $tweet, boolean $full_encode = false ) : Twitter_Autolink
$tweet string The tweet to be converted.
$full_encode boolean Whether to encode all special characters.
return Twitter_Autolink

getCashtagClass() public method

CSS class for auto-linked cashtag URLs.
public getCashtagClass ( ) : string
return string CSS class for cashtag links.

getExternal() public method

Often this is used to be matched on in JavaScript for dynamically adding the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has been undeprecated and thus the 'target' attribute can be used. If this is set to false then the 'target' attribute will be output.
public getExternal ( ) : boolean
return boolean Whether to add 'external' to the 'rel' attribute.

getHashtagClass() public method

CSS class for auto-linked hashtag URLs.
public getHashtagClass ( ) : string
return string CSS class for hashtag links.

getListClass() public method

CSS class for auto-linked username/list URLs.
public getListClass ( ) : string
return string CSS class for username/list links.

getNoFollow() public method

Whether to include the value 'nofollow' in the 'rel' attribute.
public getNoFollow ( ) : boolean
return boolean Whether to add 'nofollow' to the 'rel' attribute.

getTarget() public method

Support for the 'target' attribute was deprecated in HTML 4.01 but has since been reinstated in HTML 5. To output the 'target' attribute you must disable the adding of the string 'external' to the 'rel' attribute.
public getTarget ( ) : string
return string The value to add to the 'target' attribute.

getURLClass() public method

CSS class for auto-linked URLs.
public getURLClass ( ) : string
return string CSS class for URL links.

getUsernameClass() public method

CSS class for auto-linked username URLs.
public getUsernameClass ( ) : string
return string CSS class for username links.

setCashtagClass() public method

CSS class for auto-linked cashtag URLs.
public setCashtagClass ( string $v ) : Twitter_Autolink
$v string CSS class for cashtag links.
return Twitter_Autolink Fluid method chaining.

setExternal() public method

Often this is used to be matched on in JavaScript for dynamically adding the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has been undeprecated and thus the 'target' attribute can be used. If this is set to false then the 'target' attribute will be output.
public setExternal ( boolean $v ) : Twitter_Autolink
$v boolean The value to add to the 'target' attribute.
return Twitter_Autolink Fluid method chaining.

setHashtagClass() public method

CSS class for auto-linked hashtag URLs.
public setHashtagClass ( string $v ) : Twitter_Autolink
$v string CSS class for hashtag links.
return Twitter_Autolink Fluid method chaining.

setListClass() public method

CSS class for auto-linked username/list URLs.
public setListClass ( string $v ) : Twitter_Autolink
$v string CSS class for username/list links.
return Twitter_Autolink Fluid method chaining.

setNoFollow() public method

Whether to include the value 'nofollow' in the 'rel' attribute.
public setNoFollow ( boolean $v ) : Twitter_Autolink
$v boolean The value to add to the 'target' attribute.
return Twitter_Autolink Fluid method chaining.

setTarget() public method

Support for the 'target' attribute was deprecated in HTML 4.01 but has since been reinstated in HTML 5. To output the 'target' attribute you must disable the adding of the string 'external' to the 'rel' attribute.
public setTarget ( string $v ) : Twitter_Autolink
$v string The value to add to the 'target' attribute.
return Twitter_Autolink Fluid method chaining.

setURLClass() public method

CSS class for auto-linked URLs.
public setURLClass ( string $v ) : Twitter_Autolink
$v string CSS class for URL links.
return Twitter_Autolink Fluid method chaining.

setUsernameClass() public method

CSS class for auto-linked username URLs.
public setUsernameClass ( string $v ) : Twitter_Autolink
$v string CSS class for username links.
return Twitter_Autolink Fluid method chaining.

wrap() protected method

This is a helper function to perform the generation of the link.
protected wrap ( string $url, string $class, string $element ) : string
$url string The URL to use as the href.
$class string The CSS class(es) to apply (space separated).
$element string The tweet element to wrap.
return string The tweet element with a link applied.

wrapHash() protected method

This is a helper function to perform the generation of the hashtag link.
protected wrapHash ( string $url, string $class, string $element ) : string
$url string The URL to use as the href.
$class string The CSS class(es) to apply (space separated).
$element string The tweet element to wrap.
return string The tweet element with a link applied.

Property Details

$class_cash protected_oe property

CSS class for auto-linked cashtag URLs.
protected string $class_cash
return string

$class_hash protected_oe property

CSS class for auto-linked hashtag URLs.
protected string $class_hash
return string

$class_list protected_oe property

CSS class for auto-linked list URLs.
protected string $class_list
return string

$class_url protected_oe property

CSS class for auto-linked URLs.
protected string $class_url
return string

$class_user protected_oe property

CSS class for auto-linked username URLs.
protected string $class_user
return string

$external protected_oe property

Often this is used to be matched on in JavaScript for dynamically adding the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has been undeprecated and thus the 'target' attribute can be used. If this is set to false then the 'target' attribute will be output.
protected bool $external
return boolean

$nofollow protected_oe property

Whether to include the value 'nofollow' in the 'rel' attribute.
protected bool $nofollow
return boolean

$target protected_oe property

Support for the 'target' attribute was deprecated in HTML 4.01 but has since been reinstated in HTML 5. To output the 'target' attribute you must disable the adding of the string 'external' to the 'rel' attribute.
protected string $target
return string

$url_base_cash protected_oe property

URL base for cashtag links (the hashtag without the $ will be appended).
protected string $url_base_cash
return string

$url_base_hash protected_oe property

URL base for hashtag links (the hashtag without the # will be appended).
protected string $url_base_hash
return string

$url_base_list protected_oe property

URL base for list links (the username/list without the @ will be appended).
protected string $url_base_list
return string

$url_base_user protected_oe property

URL base for username links (the username without the @ will be appended).
protected string $url_base_user
return string