1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

[ask] Regex : Mengextrak link didalam html

Discussion in 'Pemrograman Web' started by amild, Nov 29, 2012.

Tags:
Thread Status:
Not open for further replies.
  1. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    langsung saja yah mastah...
    saya ingin mengektrak link yang ada dalam html, misalnya :
    Code:
    <a href="/home" target="_blank"><h2 style="margin-top:-4px">judul</h2></a>
    jadi nantinya hanya menghasilkan
    Code:
    http://www.namasitus.com/home
    plissss diajarin caranya yah mastah dengan menggunakan regex
     
  2. dimasku

    dimasku Super Hero

    Joined:
    Aug 6, 2012
    Messages:
    1,296
    Likes Received:
    151
    Location:
    Surabaya
    klo regex nyerah deh ane..

    bagusan pake simple html DOM
     
  3. nababan

    nababan Ads.id Fan

    Joined:
    Mar 8, 2011
    Messages:
    136
    Likes Received:
    57
    Location:
    Flashdisk
    PHP:
    <?php
    function regex($text) {
    $hasil str_replace('<a href="(.*?)"'$text);
    $hasil $hasil[1];
    return 
    $hasil;
    }
    ?>
    jadi tinggal masukin ke function regex, misalnya kalau alamat link situs adalah
    Code:
    <a href="http://www.google.com">Link</a>
    maka:
    PHP:
     echo regex('<a href="http://www.google.com">Link</a>');
     
  4. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    masih gak bisa mas...ceritanya nih mau grab link di halaman
    HTML:
    <a href="/home" target="_blank">
    nah nanti hasinya jadi ada tambahan
    PHP:
    www.namasitus.com/home
     
  5. thehoster

    thehoster Ads.id Pro

    Joined:
    Feb 14, 2010
    Messages:
    468
    Likes Received:
    61
    heheu coba gene bro

    Code:
    <?php
    $urL="http://JasaWebsite.co";  // set targer yang mau di grab
    
    function domain($url){
        preg_match("/^(http:\/\/)?([^\/]+)/i",$url, $matches);
        $www = $matches[2];
        preg_match("/[^\.\/]+\.[^\.\/]+$/", $www, $matches);
        return $matches[0];
        }    
    function ceklink($link){
        $regex='|<a.*?href="(.*?)"|';
        preg_match_all($regex,file_get_contents($link),$hasil);
        $links=$hasil[1];
            return $links;
        }
    $web=ceklink($urL);
    foreach ($web as $url)
    {
        if(strstr($url,domain($urL)))
        {
            echo $url."<br>";
        }else{
            echo domain($urL)."/".$url."<br>";
        }
    }
    ?>
    
     
  6. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    kalau grab cuma mau ngambil link yg gampang pake snoopy class
    Code:
    $snoopy->fetchlinks('http://www.namasitus.com/');
     
  7. nekaters

    nekaters Hero

    Joined:
    Aug 12, 2012
    Messages:
    673
    Likes Received:
    31
    Location:
    Yogyakarta
    maksud agan hasil outputnya cuma text
    HTML:
    www.namasitus.com/home
    atau merubah tag html menjadi
    HTML:
    <a href="http://www.namasitus.com/home" target="_blank">
    mungkin bisa dijelaskan dulu yg dimaksud?


    maaf ya ane msh bingung ama maksud TS :lol:
     
  8. mp3online

    mp3online Super Hero

    Joined:
    Jul 19, 2011
    Messages:
    2,228
    Likes Received:
    294
    Location:
    jakarta
    kayaknya sih maksudnya cuma text url, kayak hasil snoopy fetchlink
     
  9. amild

    amild Ads.id Pro

    Joined:
    Nov 29, 2012
    Messages:
    347
    Likes Received:
    71
    Location:
    Makassar
    iya mas nekaters, hasil outpunya berbentuk text url
    tapi masalahnya sudah fix kok dari bantuan para mastah diatas.. makasih banyak :)>-

    buat momod threadya di closed yah
     
    Last edited by a moderator: Dec 6, 2012
Thread Status:
Not open for further replies.

Share This Page