I am getting separate all url links from HTMl contents with this code
$doc = new DOMDocument();
$doc->loadHTML($string);
$anchorTags = $doc->getElementsByTagName('a');
$links = array();
foreach ($anchorTags as $url) {
$source = parse_url($url->getAttribute('href'));
$source = preg_replace('/^www\./', '', $source['host']);
$links[$source][$url->getAttribute('href')] = $url->nodeValue;
}
Output with this above code.
Array
(
[Facebook] => Array
(
[facebook.com] => https://www.facebook.com/
)
[Google] => Array
(
[google.com] => https://www.google.com/
)
[] => Array
(
[] =>
)
[yahoo] => Array
(
[yahoo.com] => https://www.yahoo.com/
)
)
I just want to get remove the null/blank elements/index/key from array for this i am using array_filter();
But not getting solution.
print_r(array_filter($links));