url-validatie-met-regex
Gesponsorde koppelingen
PHP script bestanden
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
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
<?php
function check_url($url_val) {
$url_pattern = "http\:\/\/[[:alnum:]\-\.]+(\.[[:alpha:]]{2,4})+";
$url_pattern .= "(\/[\w\-]+)*"; // folders like /val_1/45/
$url_pattern .= "((\/[\w\-\.]+\.[[:alnum:]]{2,4})?"; // filename like index.html
$url_pattern .= "|"; // end with filename or ?
$url_pattern .= "\/?)"; // trailing slash or not
$error_count = 0;
if (strpos($url_val, "?")) {
$url_parts = explode("?", $url_val);
if (!preg_match("/^".$url_pattern."$/", $url_parts[0])) {
$error_count++;
}
if (!preg_match("/^(&?[\w\-]+=\w*)+$/", $url_parts[1])) {
$error_count++;
}
} else {
if (!preg_match("/^".$url_pattern."$/", $url_val)) {
$error_count++;
}
}
if ($error_count > 0) {
return false;
} else {
return true;
}
}
// voorbeeld
if (check_url("http://www.phphulp.nl/php/scripts/5/249/")) {
echo "Adres is OK.";
} else {
echo "Adres is niet geldig";
}
?>
function check_url($url_val) {
$url_pattern = "http\:\/\/[[:alnum:]\-\.]+(\.[[:alpha:]]{2,4})+";
$url_pattern .= "(\/[\w\-]+)*"; // folders like /val_1/45/
$url_pattern .= "((\/[\w\-\.]+\.[[:alnum:]]{2,4})?"; // filename like index.html
$url_pattern .= "|"; // end with filename or ?
$url_pattern .= "\/?)"; // trailing slash or not
$error_count = 0;
if (strpos($url_val, "?")) {
$url_parts = explode("?", $url_val);
if (!preg_match("/^".$url_pattern."$/", $url_parts[0])) {
$error_count++;
}
if (!preg_match("/^(&?[\w\-]+=\w*)+$/", $url_parts[1])) {
$error_count++;
}
} else {
if (!preg_match("/^".$url_pattern."$/", $url_val)) {
$error_count++;
}
}
if ($error_count > 0) {
return false;
} else {
return true;
}
}
// voorbeeld
if (check_url("http://www.phphulp.nl/php/scripts/5/249/")) {
echo "Adres is OK.";
} else {
echo "Adres is niet geldig";
}
?>