[Script Review] Eigen CSS-HTML validator
Het oproepen van de class: $valid = new validator('http://url/');
CSS:
$css = $valid->css();
Output:
- $css['warns'] (Aantal waarschuwingen)
- $css['error'] (Aantal fouten)
- $css['total'] (Het totaal waarschuwingen + fouten)
HTML:
$html = $valid->html();
Output:
- $html['warns'] (Aantal waarschuwingen)
- $html['error'] (Aantal fouten)
- $html['total'] (Het totaal waarschuwingen + fouten)
Code (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
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
<?php
/**************************************************\
CLASS VALIDATOR
\**************************************************/
class validator {
public $check;
public $error = 0;
public $warns = 0;
public $total = 0;
// Start een nieuwe controle
public function __construct($check_uri = NULL){
$url_valid = $this->is_url($check_uri);
$is_valid = $this->url_exists($check_uri);
if ($url_valid != 1 || $is_valid != 1){
$this->check = 0;
} else {
$this->check = $check_uri;
}
}
// Controleer bestaande link
private function url_exists($myURL = NULL){
return (!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $myURL)) ? false : true;
}
// Controleer link is toegankelijk
private function is_url($myURL = NULL){
$handle = @fopen($myURL,'r');
return ($handle !== false) ? true : false;
}
// Ophalen van inhoud
private function get_contents($uri_string = NULL){
if ($contents = file_get_contents($uri_string)) {
$contents = strip_tags($contents);
$contents = preg_replace('/\s\s+/', ' ', $contents);
$contents = strtolower($contents);
}
return $contents;
}
// Controleer CSS
public function css(){
if ($this->check !== 0){
$css_url = 'http://jigsaw.w3.org/css-validator/validator?&profile=none&usermedium=all&warning=1&vextwarning=&uri=';
$css_url .= rawurlencode($this->check);
$css_get_contents = $this->get_contents($css_url);
$css_get_contents = str_replace('(', '', $css_get_contents);
$css_get_contents = str_replace(')', '', $css_get_contents);
preg_match_all('#jump to: errors (.+?) warnings (.+?) #is', $css_get_contents, $matches);
$matches['1']['0'] = isset($matches['1']['0']) ? $matches['1']['0'] : 0;
$matches['2']['0'] = isset($matches['2']['0']) ? $matches['2']['0'] : 0;
$this->error = round($matches['1']['0']);
$this->warns = round($matches['2']['0']);
$this->total = round($this->error + $this->warns);
}
}
// Controleer HTML
public function html(){
if ($this->check !== 0){
$html_url = 'http://validator.w3.org/check?charset=%28detect+automatically%29&doctype=Inline&group=0&uri=';
$html_url .= rawurlencode($this->check);
$html_get_contents = $this->get_contents($html_url);
preg_match_all('#result: (passed|(.+?) (errors)), (.+?) warning#is', $html_get_contents, $matches);
$matches['2']['0'] = isset($matches['2']['0']) ? $matches['2']['0'] : 0;
$matches['4']['0'] = isset($matches['4']['0']) ? $matches['4']['0'] : 0;
$this->error = round($matches['2']['0']);
$this->warns = round($matches['4']['0']);
$this->total = round($this->error + $this->warns);
}
}
}
$valid = new validator('http://www.phphulp.nl/');
$valid->css();
print 'CSS';
print '<pre>';
print_r($valid);
print '</pre>';
print 'HTML';
$valid->html();
print '<pre>';
print_r($valid);
print '</pre>';
?>
/**************************************************\
CLASS VALIDATOR
\**************************************************/
class validator {
public $check;
public $error = 0;
public $warns = 0;
public $total = 0;
// Start een nieuwe controle
public function __construct($check_uri = NULL){
$url_valid = $this->is_url($check_uri);
$is_valid = $this->url_exists($check_uri);
if ($url_valid != 1 || $is_valid != 1){
$this->check = 0;
} else {
$this->check = $check_uri;
}
}
// Controleer bestaande link
private function url_exists($myURL = NULL){
return (!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $myURL)) ? false : true;
}
// Controleer link is toegankelijk
private function is_url($myURL = NULL){
$handle = @fopen($myURL,'r');
return ($handle !== false) ? true : false;
}
// Ophalen van inhoud
private function get_contents($uri_string = NULL){
if ($contents = file_get_contents($uri_string)) {
$contents = strip_tags($contents);
$contents = preg_replace('/\s\s+/', ' ', $contents);
$contents = strtolower($contents);
}
return $contents;
}
// Controleer CSS
public function css(){
if ($this->check !== 0){
$css_url = 'http://jigsaw.w3.org/css-validator/validator?&profile=none&usermedium=all&warning=1&vextwarning=&uri=';
$css_url .= rawurlencode($this->check);
$css_get_contents = $this->get_contents($css_url);
$css_get_contents = str_replace('(', '', $css_get_contents);
$css_get_contents = str_replace(')', '', $css_get_contents);
preg_match_all('#jump to: errors (.+?) warnings (.+?) #is', $css_get_contents, $matches);
$matches['1']['0'] = isset($matches['1']['0']) ? $matches['1']['0'] : 0;
$matches['2']['0'] = isset($matches['2']['0']) ? $matches['2']['0'] : 0;
$this->error = round($matches['1']['0']);
$this->warns = round($matches['2']['0']);
$this->total = round($this->error + $this->warns);
}
}
// Controleer HTML
public function html(){
if ($this->check !== 0){
$html_url = 'http://validator.w3.org/check?charset=%28detect+automatically%29&doctype=Inline&group=0&uri=';
$html_url .= rawurlencode($this->check);
$html_get_contents = $this->get_contents($html_url);
preg_match_all('#result: (passed|(.+?) (errors)), (.+?) warning#is', $html_get_contents, $matches);
$matches['2']['0'] = isset($matches['2']['0']) ? $matches['2']['0'] : 0;
$matches['4']['0'] = isset($matches['4']['0']) ? $matches['4']['0'] : 0;
$this->error = round($matches['2']['0']);
$this->warns = round($matches['4']['0']);
$this->total = round($this->error + $this->warns);
}
}
}
$valid = new validator('http://www.phphulp.nl/');
$valid->css();
print 'CSS';
print '<pre>';
print_r($valid);
print '</pre>';
print 'HTML';
$valid->html();
print '<pre>';
print_r($valid);
print '</pre>';
?>
Er zijn nog geen reacties op dit bericht.