KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Obtain an array of links from an HTML
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: March 26, 2015

Below is a utility method to obtain an array of clickable links from an HTML:

// Method: GetHTMLLinks
// Description: Obtains Clickable Links from an HTML
//
// $1: Pointer to a Text Array that will contain the list of links
// $2: Text Variable containing the html
//

C_POINTER($1;$arrayText_p)
C_TEXT($2;$html_t)

If (Count parameters>1)
   C_LONGINT($bodyStart_l;$bodyEnd_l;$i;$start_l;$length_l)
   C_TEXT($refPattern_at;$linkPattern_at)
   ARRAY LONGINT($href_al;0)
   ARRAY TEXT($links_at;0)
   $arrayText_p:=$1
   $html:=$2
   $bodyStart_l:=Position("";$html)
   $bodyEnd_l:=Position("";$html)
   APPEND TO ARRAY($href_al;$bodyStart_l)

   $refPattern_at:="<a [^(href)]*href="
   $linkPattern_at:="\"[^\"]*\""
   $i:=1

   While (Match regex($refPattern_at;$html;$href_al{$i};$start_l;$length_l)
      $i:=$i+1
      If($start_l<$bodyEnd_l)
      APPEND TO ARRAY($href_al;$start_l+$length_l)
      Match regex($linkPattern_at;$html;$href_al{$i};$start_l;$length_l)
      APPEND TO ARRAY($links_at;Substring($html;$start_l+1;$length_l-2))
      End if
   End while
   COPY ARRAY($links_at;$arrayText_p->)
End if


Below is an example of the Utility used to obtain the list of clickable links from www.Google.com by obtaining the html from a Web Area.
C_TEXT($html)
WA OPEN URL(*;"Web Area";"https://www.google.com/")
$html:=WA Get page content(*;"Web Area")
ARRAY TEXT($links;0)
GetHTMLLinks(->$links;$html)


Below is the Result of the example: