Zend - The PHP Company




HTML

Add Code


Anchor Extractor  

Type: code fragment
Added by: kgourlay
Entered: 12/07/2000
Last modified: 08/12/1999
Rating: ***** (4 votes)
Views: 7559
Give it a string containing HTML code and it'll extract all the anchors (links) from it. It'll set up an array of the URLs (as they appear in the code) and another array of the related "hilighted" text. This isn't perfect, but I figured I'd toss it up since I was looking for it earlier today.


<?php
  
// before we start, put your HTML code (as a string) into $data.
  
$data "This is my <A HREF="http://thechain.com/">HTML</A> code.";

  
unset($location);
  
$links = array();  // the contents of the anchors (highlighted text)
  
$hrefs = array();  // the URLs corresponding to each element of $links
  
$pos 0;
  while (!((
$pos strpos($data,"<",$pos)) === false)) {
    
$pos++;
    
$endpos strpos($data,">",$pos);
    
$tag substr($data,$pos,$endpos-$pos);
    
$tag trim($tag);
    if (isset(
$location)) {  // look for a </A>
      
if (!strcasecmp(strtok($tag," "),"/A")) {
        
$link substr($data,$linkpos,$pos-1-$linkpos);
        
$links[] = $link;
        
$hrefs[] = $location;
        unset(
$location);
      }
      
$pos $endpos+1;
    } else {  
// look for a <A ...>
      
if (!strcasecmp(strtok($tag," "),"A")) {
        if (
eregi("HREF[ tnrv]*=[ tnrv]*"([^"]*)"",$tag,$regs));
        else if (eregi("
HREFtnrv]*=[ tnrv]*([^ tnrv]*)",$tag,$regs));
        else 
$regs[1] = "";
        if (
$regs[1]) {  // Only use it if it seems to be reasonable
          
$location = $regs[1];
        }
        
$pos = $endpos+1;
        
$linkpos = $pos;
      } else {
        
$pos = $endpos+1;
      }
    }
  }

?>


Usage Example


/* stick this after the above code and you'll
   get a list of all the links. */

  for ($i=0; $i<sizeof($links); $i++) {
    echo "<A HREF="".$hrefs[$i]."">".$links[$i]."</A><BR>n";
  }


Rate This Script





Search



This Category All Categories