2

My concern is to extract links from a div using php html dom library.

code exemple :

include("simple_html_dom.php");
$html='
<div id="base" url-data="http://www.domaine.com/page?user=username"></div>
<div id="base" url-data="http://www.domaine.info/page?user=username"></div>
<div id="base" url-data="http://www.domaine.org/page?user=username"></div>
<div id="base" url-data="http://www.domaine.net/page?user=username"></div>
<div id="base" url-data="http://www.domaine.biz/page?user=username"></div>
<div id="base" url-data="http://www.domaine.fr/page?user=username"></div>
';

I need to fetch all domains names from a div then store them in a php array using simple html dom parser exemple :

domaine.com,domaine.info,domaine.org,domaine.net,domaine.biz,domaine.fr

Thank you.

2
  • 1
    What have you tried so far? What are the issues you are having? I will help but I won't write your code for you... Commented Jun 11, 2015 at 12:51
  • this is what you want ? Commented Jun 11, 2015 at 12:58

1 Answer 1

1
include("simple_html_dom.php");
$html='
<div id="base" url-data="http://www.domaine.com/page?user=username"></div>
<div id="base" url-data="http://www.domaine.info/page?user=username"></div>
<div id="base" url-data="http://www.domaine.org/page?user=username"></div>
<div id="base" url-data="http://www.domaine.net/page?user=username"></div>
<div id="base" url-data="http://www.domaine.biz/page?user=username"></div>
<div id="base" url-data="http://www.domaine.fr/page?user=username"></div>
';
$str_html=str_get_html($html);
// $file_html=file_get_html($html); // use file_get_html if you parse an url.
$div=$str_html->find("div#base");
$count=count($div)-1;
for($a=0;$a<=$count;$a++){
    $url=$str_html->find("div#base",$a)->getAttribute('url-data');
    $parse = parse_url($url);
    $domain = $parse['host'];
    $array[]=$domain;
}
print_r($array);
Sign up to request clarification or add additional context in comments.

1 Comment

find is slow function. i think better $divs = $str_html->find("div#base"); foreach($divs as $div)....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.