-
Notifications
You must be signed in to change notification settings - Fork 6
/
DK.php
156 lines (146 loc) · 4.4 KB
/
DK.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
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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Specific validation methods for data used in DK
*
* PHP Versions 4 and 5
*
* This source file is subject to the New BSD license, That is bundled
* with this package in the file LICENSE, and is available through
* the world-wide-web at
* http://www.opensource.org/licenses/bsd-license.php
* If you did not receive a copy of the new BSDlicense and are unable
* to obtain it through the world-wide-web, please send a note to
* pajoye@php.net so we can mail you a copy immediately.
*
* @category Validate
* @package Validate_DK
* @author Jesper Veggerby <pear.nosey@veggerby.dk>
* @copyright 2003-2005 Jesper Veggerby Hansen
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Validate_DK
*/
/**
* Data validation class for Denmark
*
* This class provides methods to validate:
*
* - Postal code
* - Social Security Number (CPR Nummer)
* - Danish telephone number
* - Car registration number
*
* @category Validate
* @package Validate_DK
* @author Jesper Veggerby <pear.nosey@veggerby.dk>
* @copyright 2003-2005 Jesper Veggerby Hansen
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php. net/package/Validate_DK
*/
class Validate_DK
{
/**
* validates a postcode
*
* Four digit postal code, maybe with a leading 'DK-'
*
* @param string $postcode the postcode to be validated
* @param bool $strong optional; strong checks (e.g. against a list
* of postcodes)
*
* @access public
* @return bool
*/
function postalCode($postcode, $strong=false)
{
$preg = "/^(DK-)?[0-9]{4}$/";
$match = preg_match($preg, $postcode)? true : false;
return $match;
}
/**
* validates a CPR Number (ssn equiv)
*
* The danish CPR number is a 8 digit number with the birthdate as
* ddmmyy-xxxy where xxxy is a four digit control number.
*
* The 10 digits are summarized with coefficients 4, 3, 2, 7, 6, 5, 4, 3, 2
* and 1. If the sum is divisible by 11 the control is correct.
*
* The last digit of the control number (y) is also dependend on gender, if
* y is odd it's a male cpr number and if even a female.
*
* @param string $cpr CPR number
* @param string $gender The gender to validate for 'M' for male, 'F'
* for female, false or omitted to not perform
* the check.
*
* @access public
* @return bool
*/
function ssn($cpr, $gender = false)
{
// remove spaces and uppercase it
$preg = "/^[0-3][0-9][0-1]\d{3}-\d{4}?/";
if (preg_match($preg, $cpr)) {
$cpr = str_replace('-', '', $cpr);
$y = substr($cpr, -1);
switch ($gender) {
case 'M':
$genderOK = (($y % 2) == 1);
break;
case 'F':
$genderOK = (($y % 2) == 0);
break;
default:
$genderOK = true;
break;
}
return $genderOK;
} else {
return false;
}
}
/**
* Validate danish telephone number
*
* Simple check: 8 digits when removing (, ), -, +, ., ' '
*
* @param string $tel the tel number
*
* @access public
* @return bool
*/
function phoneNumber($tel)
{
// just checks to see if it is numeric and starts with a 0
// remove any wierd characters like (,),-,. etc
$tel = str_replace(Array('(', ')', '-', '+', '.', ' '), '', $tel);
$preg = "/^[0-9]{8}$/";
$match = (preg_match($preg, $tel)) ? true : false;
return $match;
}
/**
* Validates a car registration number
*
* Format: AA XX YYY
*
* Where AA are 2 letter UPPERCASE A-Z
*
* @param string $reg the registration number
*
* @access public
* @return bool
*/
function carReg($reg)
{
$prepreg = "/^[A-Z]{2} [0-9]{2} [0-9]{3}$/";
if (preg_match($prepreg, $reg)) {
return true;
} else {
return false;
}
}
}
?>