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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson