1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Main Class to manipulate dom |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category GLICER |
8
|
|
|
* @package GlHtml |
9
|
|
|
* @author Emmanuel ROECKER |
10
|
|
|
* @author Rym BOUCHAGOUR |
11
|
|
|
* @copyright 2015 GLICER |
12
|
|
|
* @license MIT |
13
|
|
|
* @link http://dev.glicer.com/ |
14
|
|
|
* |
15
|
|
|
* Created : 19/02/15 |
16
|
|
|
* File : GlHtml.php |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace GlHtml; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\CssSelector\CssSelector; |
24
|
|
|
use Symfony\Component\CssSelector\CssSelectorConverter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class GlHtml |
28
|
|
|
* @package GlHtml |
29
|
|
|
*/ |
30
|
|
|
class GlHtml |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var \DOMDocument |
34
|
|
|
*/ |
35
|
|
|
private $dom; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $html; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $html |
44
|
|
|
*/ |
45
|
|
|
public function __construct($html) |
46
|
|
|
{ |
47
|
|
|
$html = static::fixNewlines($html); |
48
|
|
|
$this->dom = new \DOMDocument(); |
49
|
|
|
|
50
|
|
|
$libxml_previous_state = libxml_use_internal_errors(true); //disable warnings |
51
|
|
|
$this->dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); |
52
|
|
|
libxml_clear_errors(); |
53
|
|
|
libxml_use_internal_errors($libxml_previous_state); |
54
|
|
|
|
55
|
|
|
$this->html = $html; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Unify newlines |
60
|
|
|
* |
61
|
|
|
* @param string $text |
62
|
|
|
* |
63
|
|
|
* @return string the fixed text |
64
|
|
|
*/ |
65
|
|
|
static function fixNewlines($text) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$text = str_replace("\r\n", "\n", $text); |
68
|
|
|
$text = str_replace("\r", "\n", $text); |
69
|
|
|
|
70
|
|
|
return $text; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* return one dom element with $selector css filter |
75
|
|
|
* |
76
|
|
|
* @param string $selector CSS 3 Selector |
77
|
|
|
* |
78
|
|
|
* @return GlHtmlNode[] |
79
|
|
|
*/ |
80
|
|
|
public function get($selector) |
81
|
|
|
{ |
82
|
|
|
$xpath = new \DOMXPath($this->dom); |
83
|
|
|
|
84
|
|
|
if(class_exists('Symfony\Component\CssSelector\CssSelector')) { |
85
|
|
|
$expression = CssSelector::toXPath($selector); |
86
|
|
|
} else { |
87
|
|
|
$converter = new CssSelectorConverter(); |
88
|
|
|
$expression = $converter->toXPath($selector); |
89
|
|
|
} |
90
|
|
|
$nodes = $xpath->query($expression); |
91
|
|
|
|
92
|
|
|
$glnodes = []; |
93
|
|
|
foreach ($nodes as $node) { |
94
|
|
|
$glnodes[] = new GlHtmlNode($node); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $glnodes; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* set a list of attributes |
102
|
|
|
* |
103
|
|
|
* @param string $selector |
104
|
|
|
* @param array $attributes |
105
|
|
|
* @param string $value |
106
|
|
|
*/ |
107
|
|
|
public function set($selector, array $attributes, $value = null) |
108
|
|
|
{ |
109
|
|
|
$nodes = $this->get($selector); |
110
|
|
|
|
111
|
|
|
foreach ($nodes as $node) { |
112
|
|
|
$node->set($attributes, $value); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $selector |
118
|
|
|
*/ |
119
|
|
|
public function delete($selector) |
120
|
|
|
{ |
121
|
|
|
$nodes = $this->get($selector); |
122
|
|
|
foreach ($nodes as $node) { |
123
|
|
|
$node->delete(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function html() |
131
|
|
|
{ |
132
|
|
|
return $this->dom->saveHTML(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getText() |
136
|
|
|
{ |
137
|
|
|
$body = $this->get("body")[0]; |
138
|
|
|
|
139
|
|
|
return $body->getText(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $tagname |
144
|
|
|
* @param string $attribute |
145
|
|
|
* @param array $links |
146
|
|
|
*/ |
147
|
|
|
private function getLinksByTagAttribute($tagname, $attribute, array &$links) |
148
|
|
|
{ |
149
|
|
|
$tagslink = $this->get($tagname); |
150
|
|
|
foreach ($tagslink as $taglink) { |
151
|
|
|
$href = $taglink->getAttribute($attribute); |
152
|
|
|
if (isset($href) && (strlen(trim($href)) > 0)) { |
153
|
|
|
$links[$href] = $href; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $all if true get url in text and params |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function getLinks($all = false) |
164
|
|
|
{ |
165
|
|
|
$links = []; |
166
|
|
|
|
167
|
|
|
$this->getLinksByTagAttribute("link", "href", $links); |
168
|
|
|
$this->getLinksByTagAttribute("a", "href",$links); |
169
|
|
|
$this->getLinksByTagAttribute("script", "src", $links); |
170
|
|
|
$this->getLinksByTagAttribute("iframe", "src", $links); |
171
|
|
|
$this->getLinksByTagAttribute("img", "src", $links); |
172
|
|
|
|
173
|
|
|
//get all string started with http |
174
|
|
|
$regexUrl = '/[">\s]+((http|https|ftp|ftps)\:\/\/(.*?))["<\s]+/'; |
175
|
|
|
$urls = null; |
176
|
|
|
if (preg_match_all($regexUrl, $this->html, $urls) > 0) { |
177
|
|
|
$matches = $urls[1]; |
178
|
|
|
foreach ($matches as $url) { |
179
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL)) { |
180
|
|
|
$links[$url] = $url; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($all) { |
186
|
|
|
//get all params which can be a url |
187
|
|
|
$regexParam = '/["](.*?)["]/'; |
188
|
|
|
$params = []; |
189
|
|
|
if (preg_match_all($regexParam, $this->html, $params) > 0) { |
190
|
|
|
$urls = $params[1]; |
191
|
|
|
foreach ($urls as $url) { |
192
|
|
|
$url = trim($url); |
193
|
|
|
if ((strpbrk($url, "/.") !== false) && (strpbrk($url, " ") === false)) { |
194
|
|
|
$links[$url] = $url; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
foreach ($links as $link) { |
201
|
|
|
$url = parse_url($link); |
202
|
|
|
if (!((isset($url['host']) && isset($url['scheme'])) || (isset($url['path'])))) { |
203
|
|
|
unset($links[$link]); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $links; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getSentences() |
211
|
|
|
{ |
212
|
|
|
$sentences = []; |
213
|
|
|
|
214
|
|
|
$body = $this->get("body"); |
215
|
|
|
if (count($body) > 0) { |
216
|
|
|
$sentences = $body[0]->getSentences(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$description = $this->get('meta[name="description"]'); |
220
|
|
View Code Duplication |
if (count($description) > 0) { |
|
|
|
|
221
|
|
|
$description = trim($description[0]->getAttribute("content")); |
222
|
|
|
if (strlen($description) > 0) { |
223
|
|
|
array_unshift($sentences, $description); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$title = $this->get('title'); |
228
|
|
View Code Duplication |
if (count($title) > 0) { |
|
|
|
|
229
|
|
|
$title = trim($title[0]->getText()); |
230
|
|
|
if (strlen($title) > 0) { |
231
|
|
|
array_unshift($sentences, $title); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $sentences; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return GlHtmlSummary[] |
240
|
|
|
*/ |
241
|
|
|
public function getSummary() |
242
|
|
|
{ |
243
|
|
|
$body = $this->get("body")[0]; |
244
|
|
|
|
245
|
|
|
$summary = []; |
246
|
|
|
$callback = function (GlHtmlNode $childNode) use (&$summary) { |
247
|
|
|
$nodeName = $childNode->getName(); |
248
|
|
|
|
249
|
|
|
if (preg_match('/^h(\d+)$/', $nodeName, $matches)) { |
250
|
|
|
$summary[] = new GlHtmlSummary($childNode, $matches[1]); |
251
|
|
|
} |
252
|
|
|
}; |
253
|
|
|
|
254
|
|
|
$body->callChild($callback); |
255
|
|
|
|
256
|
|
|
return $summary; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.