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(“https://”,$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(“https://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 Comments

  1. Jamal Soueidan

    Looks goods, but it can be more complicated then that 🙂

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

  2. tujuannya buat apa? mendingan bikin email address extractor pak.

  3. @ 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. @ Rizky
    Yeh, kan dimulai dari ini dulu Pak, biar bisa ngikutin link baru di extract alamat emailnya

    Please Translate it to english 😀

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

    *maaf, bahasa inggris saya pathing pechothot

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

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