<?php
function prepareForDOM($html, $encoding) {
$html = iconv($encoding, 'UTF-8//TRANSLIT', $html);
$html = preg_replace('/<(script|style|noscript)\b[^>]*>.*?<\/\1\b[^>]*>/is', '', $html);
$tidy = new tidy;
$config = array(
'drop-font-tags' => true,
'drop-proprietary-attributes' => true,
'hide-comments' => true,
'indent' => true,
'logical-emphasis' => true,
'numeric-entities' => true,
'output-xhtml' => true,
'wrap' => 0
);
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
$html = $tidy->value;
$html = preg_replace('#<meta[^>]+>#isu', '', $html);
$html = preg_replace('#<head\b[^>]*>#isu', "<head>\r\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />", $html);
return $html;
};
$html = file_get_contents($url);
$html = prepareForDOM($html, 'windows-1251');
$dom = new DOMDocument();
@$dom->loadHTML($html);
// Ну и начинаем работать с DOM
$nodes = $dom->getElementsByTagName('img');
?>