php data file_get_contents http www hyundai ru i40-Wagon dom create_do

 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
$data = file_get_contents("http://www.hyundai.ru/i40-Wagon");
$dom = create_dom($data);
$elements = get_elements_by_attr($dom, 'div', 'class', 'movie_thumb');
foreach ($elements as $el) {
$img = $el->getElementsByTagName('img');
if ($img->length > 0)
echo $img->item(0)->getAttribute('data-original')."\n";
}
function create_dom($res) {
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->strictErrorChecking = false;
$doc->encoding = 'UTF-8';
$res = preg_replace("/<!--(.*?)-->/s", "", $res);
@$doc->loadHTML('<?xml version="1.1" encoding="UTF-8" ?>'.$res);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//comment()') as $comment)
$comment->parentNode->removeChild($comment);
$scripts = $doc->getElementsByTagName('script');
foreach ($scripts as $script)
$script->parentNode->removeChild($script);
$styles = $doc->getElementsByTagName('style');
foreach ($styles as $style)
$style->parentNode->removeChild($style);
return $doc;
}
function get_elements_by_attr($dom, $tagname, $attr, $value) {
$result = array();
$elements = $dom->getElementsByTagName($tagname);
foreach ($elements as $element) {
if ($tagname !== NULL && $element->nodeName != $tagname)
continue;
if ($element->getAttribute($attr) == $value)
$result[] = $element;
}
return $result;
}
function get_elements_by_attr_re($dom, $tagname, $attr, $value) {
$result = array();
$elements = $dom->getElementsByTagName($tagname);
foreach ($elements as $element) {
if ($tagname !== NULL && $element->nodeName != $tagname)
continue;
if (preg_match($value, $element->getAttribute($attr)) > 0)
$result[] = $element;
}
return $result;
}
function get_element_by_attr_re($dom, $tagname, $attr, $value) {
$elements = $dom->getElementsByTagName($tagname);
foreach ($elements as $element) {
if ($tagname !== NULL && $element->nodeName != $tagname)
continue;
if (preg_match($value, $element->getAttribute($attr)) > 0)
return $element;
}
return NULL;
}