27/08/2007
PHP

Extract link from specific page/URL

This is a simple function to extract link from a specific page/URL :
function extract_url($main_url){$cek_url = parse_url($main_url);

$prefix_url = $cek_url[‘scheme’].’://’.$cek_url[‘host’];

$f = fopen($main_url,”r”);

$inputStream = fread($f,65535);

fclose($f);

if (preg_match_all(“/(.*?)</a>/i”,$inputStream,$matches)) {

foreach($matches[1] as $link){

if(!eregi(‘mailto:|javascript:|ymsgr:’,$link)){

if(eregi(“http://”,$link)){

$url = $link;

}

else{

$url = $prefix_url.$link;

}

if(eregi(‘PHPSESSID’,$url)){

$url = explode(“PHPSESSID”,$url);

$url = substr($url[0],0,-1);

}

$output[] = $url;

}

}

}

return array_unique($output);

}

?>

And here is sample how to use the function:
$start = time();

print_r(extract_url(“http://dev.sandalian.com/”));

$end = time();echo ‘done in ‘.($end-$start).’ second’;

?>
This script only working when remote_url are allowed. Otherwise you will need to use CURL.

7 thoughts on “Extract link from specific page/URL
  1. Looks goods, but it can be more complicated then that ๐Ÿ™‚

    When its comes to extract url from the whole domain (subpages) etc. ๐Ÿ™‚

  2. @ Jamal Soueidan
    Yupe, but this is the basic function to do all that stuffs.

    @ Rizky
    Yeh, kan dimulai dari ini dulu Pak, biar bisa ngikutin link baru di extract alamat emailnya

  3. @ Rizky
    Yeh, kan dimulai dari ini dulu Pak, biar bisa ngikutin link baru di extract alamat emailnya

    Please Translate it to english ๐Ÿ˜€

  4. why dont use CURL since u know that FOPEN is more possible to disable

    *maaf, bahasa inggris saya pathing pechothot

Comments are closed.