?
Solved

could you explain this cfscript please?

Posted on 2006-04-26
4
Medium Priority
?
356 Views
Last Modified: 2013-12-24
been searching on this site for some info on using CFHTTP, found this code (below) which pulls links, works greate.

could any one explain the bits in the cfscript tag line by line? it seems to do so much in so little code!!!

oany please is in the in array dump it shows the links and the link description, but i dont see where this is in the script.

<cfscript>
function parseURLs(obj)
{
   arr=arraynew(2);set=structnew();
   set.pos=arraynew(1);set.pos[1] = 0;
   while(true){
      set=refindnocase("<a[^>]+href=[""']([^""']*)[""'][^>]*>([^<]*)</a>",obj,set.pos[arraylen(set.pos)]+1,true);
      if(arraylen(set.len) lt 3) break;
           arr[arraylen(arr)+1][1]=mid(obj,set.pos[2],set.len[2]);
           arr[arraylen(arr)][2]=mid(obj,set.pos[3],set.len[3]);
    }
   return arr;
}
</cfscript>

<cfhttp method='get' url='http://google.com'></cfhttp>
<cfset NewArray = parseURLs(cfhttp.FileContent)>
<cfdump var="#NewArray#">  

thanks in advance :)
0
Comment
Question by:pigmentarts
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
mkishline earned 2000 total points
ID: 16548249
<cfscript>
function parseURLs(obj)
{
      <!--- Initialize variables --->
      arr=arraynew(2);set=structnew();
      <!--- Create a one-dimensional array and name it "pos" --->
      set.pos=arraynew(1);
      <!--- Set the first value of the "pos" or position array to 0 --->
      set.pos[1] = 0;
      <!--- Loop --->
      while(true){
            <!--- search for a link (<a href...) tag in the html (passed in as obj to the function) starting at the position+1 specified in the last element of the set.pos array --->
            set=refindnocase("<a[^>]+href=[""']([^""']*)[""'][^>]*>([^<]*)</a>",obj,set.pos[arraylen(set.pos)]+1,true);
            <!--- if it did not find a valid link then end the loop --->
            if(arraylen(set.len) lt 3) break;
            
            <!--- if it did find a valid link --->
            arr[arraylen(arr)+1][1]=mid(obj,set.pos[2],set.len[2]); <!--- add the value of the href attributes --->
            arr[arraylen(arr)][2]=mid(obj,set.pos[3],set.len[3]); <!--- add the string between the starting and ending <a> tags --->
      }
      <!--- Return the links from the page --->
      return arr;
}
</cfscript>
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 16548278
thanks for taking the time to explain this script to me, will review and play around with it. can i just ask with it been a one-dimensional array how come it holds 2 values for each position, or am i gettting this all wrong?
0
 
LVL 12

Expert Comment

by:mkishline
ID: 16548319
The set.pos variable is a one-dimensional array, but the two values that you're looking at are being stored in the two dimensional arr variable. The set structure is never returned and really serves no other real purpose than to hold the returned values from the REFindNoCase function.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 16548369
o i see, assumed it was a two dimensional array just got set.pos confused with arr. thanks for the help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question