CFLib.org – Common Function Library Project

twitterify(tweet)

Last updated December 15, 2012

author

John Venable

Version: 1 | Requires: CF5 | Library: StrLib

Description:
when you retrieve a tweet from the Twitter API, the text doesn't have links to mentions(@), hashtags(#), or URLs. This function uses regular expressions to parse the tweet and link to all three types of references.

Return Values:
Returns a string.

Example:

<cfset tweet='@bennadel The www.BenNadel.com job board made its 79th Kiva donation on behalf of "Web Developer" http://bjam.in/jobs-160 ##FullTime ##Jobs ##NJ'>

<cfoutput>

    #twitterify(tweet)#

</cfoutput>

Parameters:

Name Description Required
tweet String to parse. Yes

Full UDF Source:

/**
 * Converts hashtags, mentions, and embedded URLs to clickable links.
 * 
 * @param tweet      String to parse. (Required)
 * @return Returns a string. 
 * @author John Venable (johnnykrisma@gmail.com) 
 * @version 1, December 15, 2012 
 */
function twitterify(tweet) {
    tweet = rereplacenocase(tweet, '(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)', '\1<a href="\2" target="_blank">\2</a>', 'ALL');
    tweet = rereplacenocase(tweet, '(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)','\1<a href="http://\2" target="_blank">\2</a>','ALL');
    tweet = rereplacenocase(tweet, '##(\w+)', '<a class="tweet-hashtag" href="http://search.twitter.com/search?q=\1" target="_blank">##\1</a>', 'ALL');
    tweet = rereplacenocase(tweet, '@(\w+)', '<a class="tweet-mention" href="http://twitter.com/\1">@\1</a>', 'ALL');
        
    return tweet;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson