Location: PHPKode > scripts > Fetch Link Class > fetch-link-class/fetch.class.php
<?php
//***********************************************************************
//Author  : Rahman Haqparast
//Contact : hide@address.com
//License : Free for Non-commercial use
//Date    : April 2003
//Edited  : April 2005
//***********************************************************************
class fetchlinks
{
// Class Properties
// Private Varibales
	var $_isolated_links;
	var $_txt;
// Public Varibales
	var $href;
	var $caption;
	var $url;
// Constructor
// The constructor gets the url you want to retrieve its links
	function fetchlinks($_url)
	{
		$this->_txt=file_get_contents($_url);
		$this->_txt=str_replace("<A",'<a',$this->_txt);
		$this->_txt=str_replace("</A>",'</a>',$this->_txt);
		$this->url=$_url;
	}
// isolating <a>...</a>
// The _isolate() function isolates every <a>....</a> from the main text
// scope : Local 
	function _isolate()
		{
			$j = 0;
			for ($i=0;$i<=strlen($this->_txt);$i++)
			{
	
				if (substr($this->_txt,$i,1+1)=="<a") 
				{
					$j++;
					$st=$i;
					$k=$i;
					while (substr($this->_txt,$k,3)!="/a>")
					{
						$k++;
					}
					$en=$k+2+1;
					$this->_isolated_links[$j]=substr($this->_txt,$st,$en-$st);
				}
				
			}
		}		
// get_links() function extracts link data from the isolated links
// It first calls the _isolate() function and works on the isolated data
// scope : Global
	function get_links()
		{
		$this->_isolate();
			$k=0;
			$n=0;
			for ($i=1;$i<=count($this->_isolated_links);$i++)
			{
			
				for ($j=1;$j<=strlen($this->_isolated_links[$i]);$j++)
				{
// in this part we'll find what is written in href part of <a> link
					if (substr($this->_isolated_links[$i],$j,5)=='href=') 
					{
						$n++;
						$st=$j+5;
						$m=$j+5;
							while (substr($this->_isolated_links[$i],$m,1)!='>')
							{
								$m++;
							}
							$en=$m;
							$temp=substr($this->_isolated_links[$i],$st,$en-$st);
							if (strpos($temp,' ')>0) $temp=substr($temp,0,strpos($temp,' '));			
							$temp=str_replace('"','',$temp);
							$temp=str_replace("'","",$temp);
							if (substr($temp,0,1)=="/") $temp="$this->url".$temp;
							if (substr($temp,0,7)!="http://") $temp="$this->url/".$temp;
							if (!isset($p)) $p=0;
							$this->href[0]="";
							if (substr($temp,0,7)=="http://" && $temp!=$this->href[$p]) 
							{
								$p++;
								$this->href[$p]=$temp;
							}
//********************************************************************************					
// in this part we'll find link captions or in other words anything between link tag <a ....>---</a>
							while (substr($this->_isolated_links[$i],$m,1)!='>')
							{
								$m++;
							}
							$st=$m+1;
							while (substr($this->_isolated_links[$i],$m,4)!='</a>')
							{
								$m++;
							}
							$en=$m;
							if (substr($temp,0,7)=="http://") 
							{
								$this->caption[$p]=substr($this->_isolated_links[$i],$st,$en-$st);
							}
					}
				}
			}
		}
// This function will show the target of the links or in other words the href part of the <a> tag	
// if $mode is set to "0" the links will be shown without indicating numbers
// if $mode is set to anything else the links  will be shown with indicating numbers and "-->" sign
	function show_href($mode)
		{
		$this->get_links();
			for ($i=1;$i<count($this->href);$i++)
			{
					if ($mode==0) echo $this->href[$i]."\n<br>\n"; else echo $i."-->".$this->href[$i]."\n<br>\n";
			}
		}	
// This function will show the caption of the links.
// if $mode is set to "0" the links will be shown without indicating numbers
// if $mode is set to anything else the links  will be shown with indicating numbers and "-->" sign
	function show_caption($mode)
		{
		$this->get_links();
			for ($i=1;$i<=count($this->caption);$i++)
			{
				if ($mode==0) echo $this->caption[$i]."\n<br>\n"; else echo $i."-->".$this->caption[$i]."\n<br>\n";
			}
		}	
}
?>
Return current item: Fetch Link Class