php function prepareForDOM html encoding html iconv encoding UTF-8 TRA

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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');
?>