-
Notifications
You must be signed in to change notification settings - Fork 2
/
Doc.php
136 lines (123 loc) · 2.8 KB
/
Doc.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace H0gar\Xpath;
/**
* Xpath handler for the whole document.
*
* @author Michel Hognerud <michel@hognerud.net>
*/
class Doc {
protected $code;
protected $xpath;
/**
* Constructor
*
* @param String code The html/xml code to be parsed.
* @param String type html|xml
* @param String encoding
*/
public function __construct($code, $type='html', $encoding='UTF-8') {
$this->code = $code;
$this->xpath = static::toXpath($code, $type, $encoding);
}
public function remove($path) {
if($this->xpath === null)
return null;
$items= $this->items($path);
foreach($items as $item)
$item->remove();
return $this;
}
/**
* Returns the xpath object.
*
* @return \DOMXPath
*/
public function getXpath() {
return $this->xpath;
}
/**
* Returns the document code.
*
* @return string The parsed code.
*/
public function getCode() {
return $this->code;
}
/**
* Returns the html code for a specific children
*
* @param string path Path to the node.
* @param integer pos Element position.
*
* @return string The html code of the node.
*/
public function html($path=null, $pos=0) {
if($path === null)
return $this->code;
return $this->item($path, $pos)->html();
}
/**
* Returns text for a specific children.
*
* @param string path Path to the node.
* @param integer pos Element position.
*
* @return string The text of the node.
*/
public function text($path=null, $pos=0) {
if($path === null) {
$res = '';
foreach($this->items('/*') as $i)
$res .= $i->text();
return $res;
}
else
return $this->item($path, $pos)->text();
}
/**
* Returns the first Node object from xpath.
*
* @param string path Path to the node.
* @param integer pos Element position.
*
* @return Node
*/
public function item($path, $pos=0) {
return new Node($this->xpath->evaluate($path)->item($pos));
}
/**
* Returns an array of node objects from xpath.
*
* @param string path Path to the nodes.
*
* @return array Array of Node objects.
*/
public function items($path) {
$r = array();
$nodes = $this->xpath->evaluate($path);
foreach($nodes as $n)
$r[] = new Node($n);
return $r;
}
/**
* Convert xml/html code to a DOMXpath object.
*
* @param String code The html/xml code to be parsed.
* @param String type html|xml
* @param String encoding
*
* @return \DOMXPath
*/
protected static function toXpath($code, $type='html', $encoding='UTF-8') {
$doc = new \DOMDocument();
if ($type == 'xml')
@$doc->loadXML('<?xml encoding="'.$encoding.'">' . $code);
else
@$doc->loadHTML('<?xml encoding="'.$encoding.'">' . $code);
foreach($doc->childNodes as $item) {
if($item->nodeType == XML_PI_NODE)
$doc->removeChild($item); #remove encoding node
}
return new \DOMXPath($doc);
}
}