|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Util |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* A powerful tool for HTML or XML document manipulation and extraction of data. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Util |
|
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Dom extends \DOMDocument |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Retries a list of attributes-value which can live inside a HTML/XML-Element |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $tag A HTML/XML tag-name representation for the HTML/XML-Element |
|
23
|
|
|
* @param string $attribute A attribute inside of the HTML/XML-Element |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function fetchValues($tag, $attribute) |
|
28
|
|
|
{ |
|
29
|
|
|
$values = array(); |
|
30
|
|
|
|
|
31
|
|
|
// loop through each tag in the dom and add it to the array |
|
32
|
|
|
foreach ($this->getElementsByTagName($tag) as $element) { |
|
33
|
|
|
/* @var $element \DOMElement */ |
|
34
|
|
|
$values[] = $element->getAttribute($attribute); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $values; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Grab all links in a page |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getURLs() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->fetchValues('a', 'href'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Grab all URLs of an image |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getImageURLs() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->fetchValues('img', 'src'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Grab all URLs of an external script file like JS |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getScriptURLs() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->fetchValues('script', 'src'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Grab all URLs location of the linked document like CSS |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getCssURLs() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->fetchValues('link', 'href'); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|