I thought I would add a quick feature to my blog today. I wanted to be able to display the number of words per post. This was way easier than I thought it would be and it was mainly because of the power behind Regular Expressions. I am still new to regex so this may not be completely right but it works so I am going with it. To add this feature to your blog you have to add 1 line in your index on the same line where it displays the date and tags, here is the code.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cfoutput>| Word Count: #application.blog.getWordCount(body)#</cfoutput>
1<cfoutput>| Word Count: #application.blog.getWordCount(body)#</cfoutput>
Then you need to add the following function to the main BlogCFC Component. All we are doing here is reading the entry text, stripping the html tags and counting the words using Java's String method split. The split method actually returns an array of words so a quick arrayLen() will give us the count we are looking for.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cffunction name="getWordCount" access="public" output="true" returntype="numeric">
<cfargument name="str" type="string" required="true">
<cfset var stripped = REReplace(arguments.str, "<.*?>", "", "all")>
<cfset var words = stripped.split('\W')>
<cfreturn arrayLen(words)>
</cffunction>
1<cffunction name="getWordCount" access="public" output="true" returntype="numeric">
2 <cfargument name="str" type="string" required="true">
3 <cfset var stripped = REReplace(arguments.str, "<.*?>", "", "all")>4 <cfset var words = stripped.split('\W')>5 <cfreturn arrayLen(words)>6</cffunction>
Finally just clear the cache by appending ?reinit=1 to the url.
This entry was posted on August 9, 2007 at 3:26 PM and has received 4575 views. Comments 5 |
Print this entry.
#1 by Todd Rafferty on 8/9/07 - 3:09 PM
#2 by Dan on 8/9/07 - 3:15 PM
#3 by Rey Bango on 8/11/07 - 6:04 PM
jQuery wordStats: Tries to determine what a page is about by computing the density of its keywords.
http://www.hovinne.com/blog/index.php/2007/08/09/1...
DynaCloud – a dynamic JavaScript tag/keyword cloud with jQuery
http://johannburkard.de/blablog/programming/javasc...
I just thought these were cool plugins...well, in reality I'm trying to convert you to jQuery (www.jquery.com)! LOL!
#4 by Dan Vega on 8/16/07 - 3:09 PM
You may convert me to jquery one day. I was just checking out the tablesorter demo and I am really digging it.
#5 by Sebastiaan on 11/8/11 - 10:27 AM
Tried this function but it consequently delivers a higher wordcount than M$ Office or OpenOffice/Libre Writer. Using this simple codesnippet I get a correct wordcount:
<cfset wordcount = ListLen(queryname.textcolumn," ")>
<cfoutput>#wordcount#</cfoutput>
Any ideas why this function goes wrong?