-
Notifications
You must be signed in to change notification settings - Fork 4
/
simpleHtmlDom.php
90 lines (77 loc) · 1.61 KB
/
simpleHtmlDom.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
<?php
/**
* Easy parssing markup languages.
*
* @package SimpleHtmlDom
* @version 1.0
* @author Mahmoud Adel <[email protected]>
* @link https://github.com/EngMAF/laravel-SimpleHtmlDom
*/
require_once(__DIR__ . DS . 'libraries' . DS . 'simple_html_dom.php');
class SimpleHtmlDom {
private $dom;
private $array;
private $error;
/**
* take the file url and make a simpleHtmlDom object
*
* @param string $url
* @return object
*/
public static function url($url)
{
$shd = new SimpleHtmlDom();
$shd->dom = file_get_html($url);
if(!$shd->dom)
$shd->error = 'Failed to open stream: HTTP request failed!'; // Typical 404
return $shd;
}
/**
* Get the array of node attribute value
*
* @param string $node
* @param string $attribute
* @return array
*/
public function get($nodes, $attribute = 'innertext')
{
$this->array = array();
foreach ($this->dom->find($nodes) as $node) {
$this->array[] = trim( $node->$attribute );
}
return $this;
}
/**
* Get the all elemrents of array of node attribute value
*
* @return array
*/
public function all()
{
if(empty($this->array))
return null;
return $this->array;
}
/**
* Get the first elemrent of array of node attribute value
*
* @return string
*/
public function first()
{
if(empty($this->array))
return null;
reset($this->array);
return current($this->array);
}
/**
* Get error that might have occurred while getting content of supplied
* URL
*
* @return string
*/
public function get_error()
{
return $this->error;
}
}