Validate_DK
[ class tree: Validate_DK ] [ index: Validate_DK ] [ all elements ]

Source for file DK.php

Documentation is available at DK.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4. /**
  5.  * Specific validation methods for data used in DK
  6.  *
  7.  * PHP Versions 4 and 5
  8.  *
  9.  * This source file is subject to the New BSD license, That is bundled
  10.  * with this package in the file LICENSE, and is available through
  11.  * the world-wide-web at
  12.  * http://www.opensource.org/licenses/bsd-license.php
  13.  * If you did not receive a copy of the new BSDlicense and are unable
  14.  * to obtain it through the world-wide-web, please send a note to
  15.  * pajoye@php.net so we can mail you a copy immediately.
  16.  *
  17.  * @category  Validate
  18.  * @package   Validate_DK
  19.  * @author    Jesper Veggerby <pear.nosey@veggerby.dk>
  20.  * @copyright 2003-2005 Jesper Veggerby Hansen
  21.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  22.  * @version   CVS: $Id: DK.php,v 1.4 2007/08/12 23:48:22 kguest Exp $
  23.  * @link      http://pear.php.net/package/Validate_DK
  24.  */
  25.  
  26. /**
  27.  * Data validation class for Denmark
  28.  *
  29.  * This class provides methods to validate:
  30.  *
  31.  * - Postal code
  32.  * - Social Security Number (CPR Nummer)
  33.  * - Danish telephone number
  34.  * - Car registration number
  35.  *
  36.  * @category  Validate
  37.  * @package   Validate_DK
  38.  * @author    Jesper Veggerby <pear.nosey@veggerby.dk>
  39.  * @copyright 2003-2005 Jesper Veggerby Hansen
  40.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  41.  * @version   Release: @package_version@
  42.  * @link      http://pear.php. net/package/Validate_DK
  43.  */
  44. {
  45.  
  46.     /**
  47.      * validates a postcode
  48.      *
  49.      * Four digit postal code, maybe with a leading 'DK-'
  50.      *
  51.      * @param string $postcode the postcode to be validated
  52.      * @param bool   $strong   optional; strong checks (e.g. against a list
  53.      *                          of postcodes)
  54.      *
  55.      * @access    public
  56.      * @return    bool 
  57.      */
  58.     function postalCode($postcode$strong=false)
  59.     {
  60.         $preg  "/^(DK-)?[0-9]{4}$/";
  61.         $match preg_match($preg$postcode)? true : false;
  62.         return $match;
  63.     }
  64.  
  65.     /**
  66.      * validates a CPR Number (ssn equiv)
  67.      *
  68.      * The danish CPR number is a 8 digit number with the birthdate as
  69.      * ddmmyy-xxxy where xxxy is a four digit control number.
  70.      *
  71.      * The 10 digits are summarized with coefficients 4, 3, 2, 7, 6, 5, 4, 3, 2
  72.      * and 1. If the sum is divisible by 11 the control is correct.
  73.      *
  74.      * The last digit of the control number (y) is also dependend on gender, if
  75.      * y is odd it's a male cpr number and if even a female.
  76.      *
  77.      * @param string $cpr    CPR number
  78.      * @param string $gender The gender to validate for 'M' for male, 'F'
  79.      *                        for female, false or omitted to not perform
  80.      *                        the check.
  81.      *
  82.      * @access    public
  83.      * @return    bool 
  84.      */
  85.     function ssn($cpr$gender = false)
  86.     {
  87.         static $control = array(4327654321);
  88.  
  89.         // remove spaces and uppercase it
  90.         $preg "/^[0-9]{6}\-?[0-9]{4}$/";
  91.         if (preg_match($preg$cpr)) {
  92.             $cpr           = str_replace('-'''$cpr);
  93.             $controlCipher = 0;
  94.             for ($i = 0; $i count($control)$i++{
  95.                 $controlCipher += $control[$isubstr($cpr$i1);
  96.             }
  97.             $y substr($cpr-1);
  98.             switch ($gender{
  99.             case 'M':
  100.                 $genderOK (($y % 2== 1);
  101.                 break;
  102.             case 'F':
  103.                 $genderOK (($y % 2== 0);
  104.                 break;
  105.             default:
  106.                 $genderOK = true;
  107.                 break;
  108.             }
  109.             return ((($controlCipher % 11=== 0&& ($genderOK));
  110.         else {
  111.             return false;
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Validate danish telephone number
  117.      *
  118.      * Simple check: 8 digits when removing (, ), -, +, ., ' '
  119.      *
  120.      * @param string $tel the tel number
  121.      *
  122.      * @access    public
  123.      * @return    bool 
  124.      */
  125.     function phoneNumber($tel)
  126.     {
  127.         // just checks to see if it is numeric and starts with a 0
  128.         // remove any wierd characters like (,),-,. etc
  129.         $tel   str_replace(Array('('')''-''+''.'' ')''$tel);
  130.         $preg  "/^[0-9]{8}$/";
  131.         $match (preg_match($preg$tel)) ? true : false;
  132.         return $match;
  133.     }
  134.  
  135.     /**
  136.      * Validates a car registration number
  137.      *
  138.      * Format: AA XX YYY
  139.      *
  140.      * Where AA are 2 letter UPPERCASE A-Z
  141.      *
  142.      * @param string $reg the registration number
  143.      *
  144.      * @access    public
  145.      * @return    bool 
  146.      */
  147.     function carReg($reg)
  148.     {
  149.         $prepreg "/^[A-Z]{2} [0-9]{2} [0-9]{3}$/";
  150.         if (preg_match($prepreg$reg)) {
  151.             return true;
  152.         else {
  153.             return false;
  154.         }
  155.     }
  156. }
  157.  
  158. ?>

Documentation generated on Tue, 24 Mar 2009 07:30:02 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.