What you are returning here is the number if subexpressions of the first matching word (will always be 2), NOT the wordcount. To count the number of words, you NEED to loop. Also, you will need to escape the word for regular expressions (escape chars like \$^([ and so on). Finally, you probably want to do a REFindNoCase.


Look at CountIt (http://www.cflib.org/udf.cfm?ID=304) on cflib.org, but use REFindNoCase instead of FindNoCase with the regexp in the code. There is an escape function for RegExp on cflib too.


Pascal

-----Oorspronkelijk bericht-----
Van: Tim Blair [mailto:[EMAIL PROTECTED]
Verzonden: vr 17/10/2003 13:59
Aan: CF-Talk
CC:
Onderwerp: RE: number of occurrences in a string.


Try this (watch the wrap):

<cffunction name="wordcount" returntype="numeric" output="yes"
hint="Counts the number of occurences of a given word in a given
string">
    <cfargument name="word" type="string" required="yes" hint="The word
to look for">
    <cfargument name="line" type="string" required="yes" hint="The
string to search through">
    <cfset var wordarray =
refind("[^[:alpha:]](#arguments.word#)[^[:alpha:]]", arguments.line, 1,
TRUE)>
    <cfif NOT wordarray.len[1]>
        <cfreturn 0>
    </cfif>
    <cfreturn arraylen(wordarray.len)>
</cffunction>



[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

Reply via email to