-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathurlparser.class.php
More file actions
191 lines (166 loc) · 3.42 KB
/
urlparser.class.php
File metadata and controls
191 lines (166 loc) · 3.42 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* parseURL class
*
* Very simple and basic class for parsing a url. outputs url protocol, host, path and the query string.
*
* @version 0.2
* @author Christian Weber <christian@cw-internetdienste.de>
* @link http://www.cw-internetdienste.de
*
* freely distributable under the MIT Licence
*
*/
class parseURL {
private $regex = '@\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))@';
protected $url;
//we use get method to get the values of properties
private $protocol;
private $host;
private $path;
private $query;
/**
* __construct function.
*
* @access public
* @param mixed $url
* @return int
*/
public function __construct($url) {
if(!isset($url) || empty($url) || trim($url) == '' || !is_string($url) || !$this->check($url)) { return false; }
$this->url = $url;
$this->parseURL();
}
/**
* check function.
*
* @access private
* @param mixed $url
* @return int
*/
private function check($url) {
if(function_exists('filter_var')) {
return filter_var($url, FILTER_VALIDATE_URL);
} else {
return preg_match($this->regex,$url);
}
}
/**
* parseURL function.
*
* @access private
* @return int
*/
private function parseURL() {
$data = parse_url($this->url);
$this->protocol = $data['scheme'];
$this->host = $data['host'];
$path = (isset($data['path'])) ? $data['path']:'';
$this->path = $this->parsePath($path);
$query = (isset($data['query'])) ? $data['query']:'';
$this->query = $this->parseQuery($query);
}
/**
* parsePath function.
*
* @access private
* @param mixed $path
* @return int
*/
private function parsePath($path) {
$_path = array();
if(!empty($path)) {
if(substr($path,0,1) === '/') { $path = substr($path,1); }
if(substr($path,-1) === '/') { $path = substr($path,0,-1); }
$_path = explode('/',$path);
}
return $_path;
}
/**
* parseQuery function.
*
* @access private
* @param mixed $query
* @return int
*/
private function parseQuery($query) {
$_query = array();
if(!empty($query)) {
$_q = explode('&',$query);
foreach($_q as $item) {
$_item = explode('=',$item);
$_query[$_item[0]] = (isset($_item[1])) ? $_item[1]:true;
}
}
return $_query;
}
/**
* get function.
*
* @access public
* @param mixed $type
* @return int
*/
public function get($type) {
switch(strtolower($type)) {
case 'url': return $this->get_url();
break;
case 'protocol': return $this->get_protocol();
break;
case 'host': return $this->get_host();
break;
case 'path': return $this->get_path();
break;
case 'query': return $this->get_query();
break;
default: return false;
break;
}
}
/**
* get_url function.
*
* @access public
* @return int
*/
public function get_url(){
return $this->url;
}
/**
* get_protocol function.
*
* @access public
* @return int
*/
public function get_protocol(){
return $this->protocol;
}
/**
* get_host function.
*
* @access public
* @return int
*/
public function get_host(){
return $this->host;
}
/**
* get_path function.
*
* @access public
* @return int
*/
public function get_path(){
return $this->path;
}
/**
* get_query function.
*
* @access public
* @return int
*/
public function get_query(){
return $this->query;
}
}
?>