link extractor

Cara Extrak Link Dari Html

Posted on

Gimana sih cara ekstrak link dari string atau html?

gampag kok, bisa pake 2 fungsi di bawah ini

fungsi 1:

function ambil_link_ke_array($data) {
  preg_match_all('/<a.+?href(| )=(\'|")(.+?)(\'|").*?>/s', $data, $y, PREG_PATTERN_ORDER);
  if(count($y[3]) > 0) return $y[3];
  return false;
}

fungsi 2:

function ambil_link_ke_array($string) {
  $doc = new DOMDocument();
  @$doc->loadHTML($string);
  $xpath = new DOMXpath($doc);
  $urls = $xpath->query("//a/@href");
  foreach($urls as $url) {
    $tmp[] = $url->nodeValue;
  }
  return $tmp;
}

seandainya kita punya string html yang kusut kyk begini:

alskdjfklasjfklsdf
alskdjfklasjfklsdfalskdjfklasjfklsdfalskdjfklasjfklsdf
asdfasdfasdfjasdklf<a
href='dodol.com'>alskdjfklasjfklsdf</a>
asdfjasdklfjas<a href ="asdf.com" >aklsdjf</a>kldfjklasjdfklasjdfkljasdklfjasklfj askld fjsklf jaskldfj asf
asdfjasdklfjaskldfjklasjdfklasjdfkljasdklfjasklfj askld fjsklf jaskldfj asf
<p>Test paragraph bung one <a href="satu.com">text hiji</a></p>
<p>Test paragraph roro kidul <a href="dua.com">text dua</a></p>
asdfjasdklfjaskldfjklasjdfklasjdfkljasdklfjasklfj askld fjsklf jaskldfj asf
as dfkjasdklf asdklfj sfj<p>Test paragraph trisakti <a href='tilu'>text tilu</a></p>asdfasdf aslfj lasdjf

kita bakalan dapet hasil kyk begini:

Array
(
    [0] => dodol.com
    [1] => asdf.com
    [2] => satu.com
    [3] => dua.com
    [4] => tilu
)
Advertisements