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 Responses to “Extract link from specific page/URL”

  1. Jamal Soueidan Says:

    Looks goods, but it can be more complicated then that :)

    When its comes to extract url from the whole domain (subpages) etc. :)

  2. Rizky Says:

    tujuannya buat apa? mendingan bikin email address extractor pak.

  3. Sandal Says:

    @ 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

  4. Mbah Darmo Says:

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

    Please Translate it to english :D

  5. Funkshit Says:

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

    *maaf, bahasa inggris saya pathing pechothot

  6. Sandal Says:

    @pangsit
    since I was too lazy to write CURL script :p

  7. web design Says:

    do you know how can i extract all URLS from string??

Leave a Reply