It may be more efficient to scan the string without actually copying
and replacing parts of it, or searching it more than the necessary
number of times.  Also, you may want to verify that the <a> tag has an
'href' attribute to prevent the counting of named anchors as links.
See below.  This codes scans through the string once, checking for
matches and counting them as it goes:

<cffunction name="GetLinkCount">
  <cfargument name="s" type="string">
  <cfset var count = 0>
  <cfset var i = 0>
  <cfloop condition="(i gt 0) or not count">
    <cfset i = REFindNoCase("<a\s+[^>]*?href[^>]+>.*?</a>", s, i + 1)>
    <cfif i><cfset count = count + 1></cfif>
  </cfloop>
  <cfreturn count>
</cffunction>



On 5/17/07, Bobby Hartsfield <[EMAIL PROTECTED]> wrote:
> Tom just answered your question ;-)
>
> I can think of a few other tags that use the href attribute other besides
> anchors/links by the way :-)
>
> This seems to work ok...
>
> <cfscript>
>         function linkcount(str)
>         {
>                 var links = 0;
>
>                 while(refindnocase("<a.*?<\/a>", str))
>                 {
>                         str = RemoveChars(str, refindnocase("<a.*?<\/a>",
> str), refindnocase(".*?<\/a>", str));
>                         links = links + 1;
>                 }
>                 return links;
>         }
> </cfscript>
>
> Use: #linkCount(thetext)#
>
> Basically it says:
> 1) Count the link
> 2) Remove the link
> 3) Repeat until no more links found.
>
>
> This also seems to work...
>
>
> <cfscript>
>         function linkcount(str)
>         {
>                 var links = 0;
>
>                 while(findnocase("<a ", str))
>                 {
>                         str = replacenocase(str, "<a ", "");
>                         links = links + 1;
>                 }
>                 return links;
>         }
> </cfscript>
>
> This one basically says:
> 1) count the instance of "<a "
> 2) remove the instance of "<a "
> 3) repeat until no more instances of "<a "
>
>
>
> -----Original Message-----
> From: K Simanonok [mailto:[EMAIL PROTECTED]
> Sent: Thursday, May 17, 2007 9:52 PM
> To: CF-Talk
> Subject: Re: Regular Expression to count links
>
> Thanks Charlie, looks like your solution will work, I'll test.
>
> Andy, I appreciate your suggestion even if it won't work.
>
> Tom, you may be well-intentioned, but do you realize your posts were
> useless?  A question like "Hey buddy, can you tell me where the train
> station is?" is never intended to be taken so literally that "yes" is a
> worthwhile answer.
>
>
> >yeah.  can't use multiple character delimiters in CF.  Not with native
> >CF array/list functions.
> >
> >if you use split(), you can.
> >
> ><cfset myString = "a_!_b_!_c_d!e_f" />
> ><cfset myArray = myString.split('_!_') />
> >
> ><cfoutput>
> >       #listLen(myString, '_!_')#
> >       <br />
> >       #arrayLen(myArray)#
> ></cfoutput>
> >
> >On 5/16/07, Tom Chiverton <[EMAIL PROTECTED]> wrote:
> >>
>
>
>
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Upgrade to Adobe ColdFusion MX7
The most significant release in over 10 years. Upgrade & see new features.
http://www.adobe.com/products/coldfusion?sdid=RVJR

Archive: 
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:278552
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to