my sample code is here
include 'simple_html_dom.php';
function get_all_links($url){
global $host;
$html = new simple_html_dom();
$html->load(file_get_contents($url));
foreach($html->find('a') as $a){
$host1 = parse_url($a->href);
$host = parse_url($url);
if($host1['host'] == $host['host']){
$data[] = $a->href;
}
}
return $data;
}
$links = get_all_links("http://www.example.com/");
foreach($links as $link){
echo $link."<br />";
}
When I try this code, I got this error: Notice: Undefined index: host in… What’s wrong in my code? Please suggest me some helping code, Thanks in Advance.
You need to check if the arrays contain entries for
'host'usingissetbefore assuming they exist:Or you can use
@to suppress warnings from the check.However, you’ll need to double-check that the latter works as you desire when both are missing.
Update: As the others pointed out there is also
array_key_exists. It will handlenullarray values whereasissetreturnsfalsefornullvalues.