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

Source for file FI.php

Documentation is available at FI.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2006-2007 Jani Mikkonen                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to the New BSD license, That is bundled  |
  8. // | with this package in the file LICENSE, and is available through      |
  9. // | the world-wide-web at                                                |
  10. // | http://www.opensource.org/licenses/bsd-license.php                   |
  11. // | If you did not receive a copy of the new BSD license and are unable  |
  12. // | to obtain it through the world-wide-web, please send a note to       |
  13. // | pajoye@php.net so we can mail you a copy immediately.                |
  14. // +----------------------------------------------------------------------+
  15. // | Author:  Jani Mikkonen <jani@mikkonen.info>                          |
  16. // +----------------------------------------------------------------------+
  17. //
  18. /**
  19.  * Specific validation methods for data used in Finland
  20.  * 
  21.  * @category   Validate
  22.  * @package    Validate_FI
  23.  * @author     Jani Mikkonen <jani@mikkonen.info>
  24.  * @copyright  2006-2007 Jani Mikkonen
  25.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD
  26.  * @version    CVS: $Id: FI.php,v 1.3 2007/07/05 11:31:17 janisto Exp $
  27.  * @link       http://pear.php.net/package/Validate_FI
  28.  */
  29.  
  30. // {{{ class Validate_FI
  31. /**
  32.  * Validation class for Finland
  33.  *
  34.  * This class provides methods to validate:
  35.  * - Postal Code
  36.  * - Telephone Number
  37.  * - Car License Plate Number
  38.  * - Motorbike License Plate Number
  39.  * - Personal Identity Number (HETU)
  40.  * - Unique Identification Number (SATU)
  41.  * - Business ID Number (Y-tunnus)
  42.  * - Party Identification Number (OVT-tunnus)
  43.  * - Value Added Tax Number (ALV-numero)
  44.  * - Bank Account Number (tilinumero)
  45.  * - Bank Reference Number (viitenumero)
  46.  * - Credit Card Number
  47.  * 
  48.  * @category   Validate
  49.  * @package    Validate_FI
  50.  * @author     Jani Mikkonen <jani@mikkonen.info>
  51.  * @copyright  2006-2007 Jani Mikkonen
  52.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD
  53.  * @version    Release: 0.4.0
  54.  * @link       http://pear.php.net/package/Validate_FI
  55.  */
  56. {
  57.     
  58.     // {{{ bool Validate_FI::postalCode( string $number [, bool $strong = false] )
  59.     /**
  60.      * Validate Finnish postal code.
  61.      * 
  62.      * Five digit postal code, maybe with a leading 'FI-'.
  63.      * 
  64.      * Format: XXXXX or FI-XXXXX
  65.      * 
  66.      * <code>
  67.      * <?php
  68.      * // Include the package
  69.      * require_once 'Validate/FI.php';
  70.      * 
  71.      * $postalCode = '00100';
  72.      * if ( Validate_FI::postalCode($postalCode) ) {
  73.      *     print 'Valid';
  74.      * } else {
  75.      *     print 'Not valid!';
  76.      * }
  77.      * 
  78.      * ?>
  79.      * </code>
  80.      * 
  81.      * @static
  82.      * @access      public
  83.      * @param       string  $number the postal code to be validated
  84.      * @param       bool    optional; strong checks
  85.      *                       (e.g. against a list of postal codes)
  86.      * @return      bool    true if postal code is valid, false otherwise
  87.      */
  88.     function postalCode($number$strong=false)
  89.     {
  90.         return (preg_match("/^(FI-)?[0-9]{5}$/"$number)) ? true : false;
  91.     }
  92.     // }}}
  93.     
  94.     // {{{ bool Validate_FI::phoneNumber( string $number )
  95.     /**
  96.      * Validate Finnish telephone number.
  97.      * 
  98.      * Simple check: number must be numeric when (, ), -, +, ., ' '
  99.      * chars are removed and 3-20 digit number.
  100.      * 
  101.      * <code>
  102.      * <?php
  103.      * // Include the package
  104.      * require_once 'Validate/FI.php';
  105.      * 
  106.      * $phoneNumber = '+358 40 1234567';
  107.      * if ( Validate_FI::phoneNumber($phoneNumber) ) {
  108.      *     print 'Valid';
  109.      * } else {
  110.      *     print 'Not valid!';
  111.      * }
  112.      * 
  113.      * ?>
  114.      * </code>
  115.      * 
  116.      * @static
  117.      * @access      public
  118.      * @param       string  $number the telephone number to be validated
  119.      * @return      bool    true if telephone number is valid, false otherwise
  120.      */
  121.     function phoneNumber($number){
  122.         $number str_replace(Array('('')''-''+''.'' ')''$number);
  123.         return (preg_match("/^[0-9]{3,20}$/"$number)) ? true : false;
  124.     }
  125.     // }}}
  126.     
  127.     // {{{ bool Validate_FI::carLicensePlate( string $number )
  128.     /**
  129.      * Validate Finnish car license plate number.
  130.      * 
  131.      * Format: AAA-XXX, CD-XXXX or C-XXXXX. First or only number cannot be zero.
  132.      * 
  133.      * AAA-XXX: AAA is 2-3 letters UPPERCASE A-Z + ÅÄÖ and XXX is 1-3 numbers.
  134.      * CD-XXXX: CD- and XXXX is 1-4 numbers (diplomat licence plate)
  135.      * C-XXXXX: C- and XXXXX is 1-5 numbers (other tax-free diplomat licence plate)
  136.      * 
  137.      * <code>
  138.      * <?php
  139.      * // Include the package
  140.      * require_once 'Validate/FI.php';
  141.      * 
  142.      * $carLicensePlate = 'ABC-123';
  143.      * if ( Validate_FI::carLicensePlate($carLicensePlate) ) {
  144.      *     print 'Valid';
  145.      * } else {
  146.      *     print 'Not valid!';
  147.      * }
  148.      * 
  149.      * ?>
  150.      * </code>
  151.      * 
  152.      * @static
  153.      * @access      public
  154.      * @param       string  $number the license plate number to be validated
  155.      * @return      bool    true if license plate number is valid, false otherwise
  156.      * @link        http://www.ake.fi/AKE/Rekisterointi/Suomen_rekisterikilvet/
  157.      */
  158.     function carLicensePlate($number){
  159.         // diplomat licence plate
  160.         if (preg_match("/^CD-[1-9]{1}[0-9]{0,3}$/"$number)) {
  161.             return true;
  162.         }
  163.         // other tax-free diplomat licence plate
  164.         if (preg_match("/^C-[1-9]{1}[0-9]{0,4}$/"$number)) {
  165.             return true;
  166.         }
  167.         // regular licence plate
  168.         if (preg_match("/^[A-ZÅÄÖ]{2,3}-[1-9]{1}[0-9]{0,2}$/"$number)) {
  169.             return true;
  170.         }
  171.         return false;
  172.     }
  173.     // }}}
  174.     
  175.     // {{{ bool Validate_FI::bikeLicensePlate( string $number )
  176.     /**
  177.      * Validate Finnish motorbike license plate number.
  178.      * 
  179.      * Format: AAAXXX. First or only number cannot be zero.
  180.      * 
  181.      * Where AAA is 2-3 letters UPPERCASE A-Z + ÅÄÖ and XXX is 1-3 numbers.
  182.      * Letters and numbers are actually in separate lines.
  183.      * 
  184.      * <code>
  185.      * <?php
  186.      * // Include the package
  187.      * require_once 'Validate/FI.php';
  188.      * 
  189.      * $bikeLicensePlate = 'ABC123';
  190.      * if ( Validate_FI::bikeLicensePlate($bikeLicensePlate) ) {
  191.      *     print 'Valid';
  192.      * } else {
  193.      *     print 'Not valid!';
  194.      * }
  195.      * 
  196.      * ?>
  197.      * </code>
  198.      * 
  199.      * @static
  200.      * @access      public
  201.      * @param       string  $number the license plate number to be validated
  202.      * @return      bool    true if license plate number is valid, false otherwise
  203.      * @link        http://www.ake.fi/AKE/Rekisterointi/Suomen_rekisterikilvet/
  204.      */
  205.     function bikeLicensePlate($number){
  206.         return (preg_match("/^[A-ZÅÄÖ]{2,3}(\n)?[1-9]{1}[0-9]{0,2}$/"$number)) ? true : false;
  207.     }
  208.     // }}}
  209.     
  210.     // {{{ mixed Validate_FI::pin( string $number [, bool $info = false] )
  211.     /**
  212.      * Validate Personal Identity Number (HETU).
  213.      *
  214.      * The Finnish PIN number (HETU) aka Social Security Number (SSN)
  215.      * is a 11 digit number with birthdate as ddmmyycxxxy where c is century,
  216.      * xxx is a three digit individual number and the last digit is a
  217.      * control number (y).
  218.      * 
  219.      * If xxx is odd it's a male PIN number and if even a female.
  220.      * 
  221.      * Return gender (Male or Female) and date of birth (YYYY-MM-DD) in array if
  222.      * PIN is valid, is available by switching $info (2nd parameter) to true.
  223.      * 
  224.      * Example: 010101-123N would be a male and born in 1st of January 1901.
  225.      * 
  226.      * <code>
  227.      * <?php
  228.      * // Include the package
  229.      * require_once 'Validate/FI.php';
  230.      * 
  231.      * $pin = '010101-123N';
  232.      * if ( Validate_FI::pin($pin) ) {
  233.      *     print 'Valid';
  234.      * } else {
  235.      *     print 'Not valid!';
  236.      * }
  237.      * 
  238.      * if ( $userinfo = Validate_FI::pin($pin, true) ) {
  239.      *     print_r($userinfo);
  240.      * } else {
  241.      *     print 'Not valid!';
  242.      * }
  243.      * 
  244.      * ?>
  245.      * </code>
  246.      * 
  247.      * @static
  248.      * @access      public
  249.      * @param       string  $number PIN number to be validated
  250.      * @param       bool    optional; Return gender and date of birth on success
  251.      * @return      mixed   Returns true or false if $info = false or
  252.      *                       gender (Male or Female) and date of birth (YYYY-MM-DD)
  253.      *                       in array if PIN is valid, false otherwise
  254.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#hetu1
  255.      */
  256.     function pin($number$info = false)
  257.     {
  258.         $regs           '';
  259.         $pin            strtoupper($number);
  260.         static $control = array("0","1","2","3","4","5","6","7","8","9",
  261.                                 "A","B","C","D","E","F","H","J","K","L",
  262.                                 "M","N","P","R","S","T","U","V","W","X","Y");
  263.         static $century = array('+' => "18",
  264.                                 '-' => "19",
  265.                                 'A' => "20");
  266.         if(ereg("^([0-9]{2})([0-9]{2})([0-9]{2})([+-A]{1})([0-9]{3})([0-9A-Z]{1})$",$pin,$regs)) {
  267.             // Validate date of birth. Must be a Gregorian date.
  268.             if(checkdate($regs[2],$regs[1],$century[$regs[4]].$regs[3])) {
  269.                 if($control[intval($regs[1].$regs[2].$regs[3].$regs[5])%31== $regs[6]{
  270.                     if($info{
  271.                         $gen ($regs[5]%2==1'Male' 'Female';
  272.                         $dob $century[$regs[4]].$regs[3].'-'.$regs[2].'-'.$regs[1];
  273.                         return array($gen$dob);
  274.                     else {
  275.                         return true;
  276.                     }
  277.                 }
  278.             }
  279.         }
  280.         return false;
  281.     }
  282.     // }}}
  283.     
  284.     // {{{ bool Validate_FI::finuid( string $number )
  285.     /**
  286.      * Validate Finnish Unique Identification Number (SATU).
  287.      * 
  288.      * FINUID (SATU) is a 9 digit number. The last digit is a control number.
  289.      * 
  290.      * Example: 10011187H
  291.      * 
  292.      * <code>
  293.      * <?php
  294.      * // Include the package
  295.      * require_once 'Validate/FI.php';
  296.      * 
  297.      * $finuid = '10011187H';
  298.      * if ( Validate_FI::finuid($finuid) ) {
  299.      *     print 'Valid';
  300.      * } else {
  301.      *     print 'Not valid!';
  302.      * }
  303.      * 
  304.      * ?>
  305.      * </code>
  306.      * 
  307.      * @static
  308.      * @access      public
  309.      * @param       string  $number FINUID number to be validated
  310.      * @return      bool    true if FINUID is valid, false otherwise
  311.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#satu
  312.      */
  313.     function finuid($number)
  314.     {
  315.         $regs           '';
  316.         $number         strtoupper($number);
  317.         static $control = array("0","1","2","3","4","5","6","7","8","9",
  318.                                 "A","B","C","D","E","F","H","J","K","L",
  319.                                 "M","N","P","R","S","T","U","V","W","X","Y");
  320.         if(ereg("^([0-9]{8})([0-9A-Z]{1})$",$number,$regs)) {
  321.             if($control[intval($regs[1])%31== $regs[2]{
  322.                 return true;
  323.             }
  324.         }
  325.         return false;
  326.     }
  327.     // }}}
  328.     
  329.     // {{{ bool Validate_FI::businessId( string $number )
  330.     /**
  331.      * Validate Finnish Business ID (Y-tunnus).
  332.      * 
  333.      * The Finnish Business ID number (Y-tunnus) is a 9 digit number.
  334.      * The last digit is a control number (y).
  335.      * 
  336.      * Format: xxxxxxx-y
  337.      * 
  338.      * <code>
  339.      * <?php
  340.      * // Include the package
  341.      * require_once 'Validate/FI.php';
  342.      * 
  343.      * $businessId = '1572860-0';
  344.      * if ( Validate_FI::businessId($businessId) ) {
  345.      *     print 'Valid';
  346.      * } else {
  347.      *     print 'Not valid!';
  348.      * }
  349.      * 
  350.      * ?>
  351.      * </code>
  352.      * 
  353.      * @static
  354.      * @access      public
  355.      * @param       string  $number Business ID number to be validated
  356.      * @return      bool    true if Business ID is valid, false otherwise
  357.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#y-tunnus2
  358.      */
  359.     function businessId($number)
  360.     {
  361.         if (preg_match("/^[0-9]{6,7}-[0-9]{1}$/"$number)) {
  362.             list($num$controlsplit('[-]'$number);
  363.             // Add leading zeros if number is < 7
  364.             $num str_pad($num70STR_PAD_LEFT);
  365.             $controlSum = 0;
  366.             $controlSum += (int)substr($num01)*7;
  367.             $controlSum += (int)substr($num11)*9;
  368.             $controlSum += (int)substr($num21)*10;
  369.             $controlSum += (int)substr($num31)*5;
  370.             $controlSum += (int)substr($num41)*8;
  371.             $controlSum += (int)substr($num51)*4;
  372.             $controlSum += (int)substr($num61)*2;
  373.             $controlSum $controlSum%11;
  374.             if($controlSum == 0{
  375.                 return ($controlSum == $control? true : false;
  376.             else if($controlSum >= 2 && $controlSum <= 10 {
  377.                 return ((11 - $controlSum== $control? true : false;
  378.             }
  379.         }
  380.         return false;
  381.     }
  382.     // }}}
  383.     
  384.     // {{{ bool Validate_FI::partyId( int $number )
  385.     /**
  386.      * Validate Finnish Party Identification number (OVT-tunnus).
  387.      * 
  388.      * The Finnish Party Identification number (OVT-tunnus) is a 12-17
  389.      * digit number and it is generated from Business ID.
  390.      * 
  391.      * Example: 0037AAAAAAAABBBBB, 0037 indicates Finland, AAAAAAAA is the
  392.      * Business ID and BBBBB is optional organization number.
  393.      * 
  394.      * <code>
  395.      * <?php
  396.      * // Include the package
  397.      * require_once 'Validate/FI.php';
  398.      * 
  399.      * $partyId = '003715728600';
  400.      * if ( Validate_FI::partyId($partyId) ) {
  401.      *     print 'Valid';
  402.      * } else {
  403.      *     print 'Not valid!';
  404.      * }
  405.      * 
  406.      * ?>
  407.      * </code>
  408.      * 
  409.      * @static
  410.      * @access      public
  411.      * @param       int     $number Party Identification number to be validated
  412.      * @return      bool    true if number is valid, false otherwise
  413.      * @see         Validate_FI::businessId()
  414.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#alv-numero
  415.      */
  416.     function partyId($number)
  417.     {
  418.         if (preg_match("/^[0-9]{12,17}$/"$number)) {
  419.             $countryCode substr($number04);
  420.             $controlNum  substr($number111);
  421.             $businessNum substr($number47);
  422.             $businessId  $businessNum.'-'.$controlNum;
  423.             if($countryCode == '0037' && Validate_FI::businessId($businessId)) {
  424.                 return true;
  425.             }
  426.         }
  427.         return false;
  428.     }
  429.     // }}}
  430.     
  431.     // {{{ bool Validate_FI::vatNumber( string $number )
  432.     /**
  433.      * Validate Finnish Value Added Tax number (ALV-numero).
  434.      * 
  435.      * The Finnish VAT number (ALV-numero) is maximum
  436.      * of 14 digits and is generated from Business ID.
  437.      * 
  438.      * Format: FIXXXXXXXX
  439.      * 
  440.      * <code>
  441.      * <?php
  442.      * // Include the package
  443.      * require_once 'Validate/FI.php';
  444.      * 
  445.      * $vatNumber = 'FI15728600';
  446.      * if ( Validate_FI::vatNumber($vatNumber) ) {
  447.      *     print 'Valid';
  448.      * } else {
  449.      *     print 'Not valid!';
  450.      * }
  451.      * 
  452.      * ?>
  453.      * </code>
  454.      * 
  455.      * @static
  456.      * @access      public
  457.      * @param       string  $number VAT number to be validated
  458.      * @return      bool    true if VAT number is valid, false otherwise
  459.      * @see         Validate_FI::businessId()
  460.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#alv-numero
  461.      */
  462.     function vatNumber($number)
  463.     {
  464.         $countryCode substr($number02);
  465.         $controlNum  substr($number-11);
  466.         $businessNum substr($number2-1);
  467.         $businessId  $businessNum.'-'.$controlNum;
  468.         if($countryCode == 'FI' && Validate_FI::businessId($businessId)) {
  469.             return true;
  470.         }
  471.         return false;
  472.     }
  473.     // }}}
  474.     
  475.     // {{{ bool Validate_FI::bankAccount( string $number )
  476.     /**
  477.      * Validate Finnish bank account number.
  478.      * 
  479.      * This method checks the bank account number according to
  480.      * Finnish Bank Association.
  481.      * 
  482.      * Format: XXXXXX-XXXXXXXX, 6 digits, - and 2-8 digits.
  483.      * 
  484.      * <code>
  485.      * <?php
  486.      * // Include the package
  487.      * require_once 'Validate/FI.php';
  488.      * 
  489.      * $bankAccount = '159030-776';
  490.      * if ( Validate_FI::bankAccount($bankAccount) ) {
  491.      *     print 'Valid';
  492.      * } else {
  493.      *     print 'Not valid!';
  494.      * }
  495.      * 
  496.      * ?>
  497.      * </code>
  498.      * 
  499.      * @static
  500.      * @access      public
  501.      * @param       string  $number Finnish bank account number to be validated
  502.      * @return      bool    true if bank account is valid, false otherwise
  503.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#pankkitili
  504.      * @link        http://www.pankkiyhdistys.fi/sisalto/upload/pdf/tilinrorakenne.pdf
  505.      */
  506.     function bankAccount($number)
  507.     {
  508.         if (preg_match("/^[0-9]{6}-[0-9]{2,8}$/"$number)) {
  509.             // Bank groups are identified by the first digit
  510.             $bankType   substr($number01);
  511.             // Group 1: First digit is 1, 2, 3, 6 or 8
  512.             $bankGroup1 = array('1','2','3','6','8');
  513.             // Group 2: First digit is 4 or 5
  514.             $bankGroup2 = array('4','5');
  515.             // split account number
  516.             $regs       '';
  517.             ereg("([0-9]{6})-([0-9]{2,8})"$number$regs);
  518.             if (in_array($bankType$bankGroup1)) {
  519.                 // Group 1: 999999-99999 -> 999999-00099999
  520.                 $number $regs[1str_pad($regs[2]80STR_PAD_LEFT);
  521.                 // Group 2: 999999-99999 -> 999999-90009999
  522.             else if (in_array($bankType$bankGroup2)) {
  523.                 $number $regs[1substr($regs[2]01
  524.                           str_pad(substr($regs[2]17)70STR_PAD_LEFT);
  525.             }
  526.             // Now when we have a 14 digit bank account number, we can validate it.
  527.             return Validate_FI::_mod10($number);
  528.         }
  529.         return false;
  530.     }
  531.     // }}}
  532.     
  533.     // {{{ bool Validate_FI::refNum( string $number )
  534.     /**
  535.      * Validate Finnish bank reference number.
  536.      * 
  537.      * This method checks the bank reference number according to
  538.      * Finnish Bank Association.
  539.      * 
  540.      * <code>
  541.      * <?php
  542.      * // Include the package
  543.      * require_once 'Validate/FI.php';
  544.      * 
  545.      * $refNum = '61 74354';
  546.      * if ( Validate_FI::refNum($refNum) ) {
  547.      *     print 'Valid';
  548.      * } else {
  549.      *     print 'Not valid!';
  550.      * }
  551.      * 
  552.      * ?>
  553.      * </code>
  554.      * 
  555.      * @static
  556.      * @access      public
  557.      * @param       string  $number Finnish bank reference number to be validated
  558.                             (spaces and dashes tolerated)
  559.      * @return      bool    true if reference number is valid, false otherwise
  560.      * @link        http://koti.mbnet.fi/~thales/tarkmerk.htm#viitenumero
  561.      */
  562.     function refNum($number)
  563.     {
  564.         // Remove non-numeric characters from $refnum. Only 4-20 digit number.
  565.         $refnum ereg_replace('[^0-9]+'''$number);
  566.         // The last digit is a control number.
  567.         $controlNum  substr($refnum-11);
  568.         // Subtract control number from the $refnum.
  569.         $refNumCheck substr($refnum0strlen($refnum)-1);
  570.         if(preg_match("/^[0-9]{3,19}$/"$refNumCheck)) {
  571.             // remove leading zeros
  572.             $refNumCheck ltrim($refNumCheck0);
  573.             $mul = 7;
  574.             $refSum = 0;
  575.             for($refLength strlen($refNumCheck)$refLength > 0; $refLength--{
  576.                 $refSum += substr($refNumCheck$refLength - 11$mul;
  577.                 switch ($mul{
  578.                 case 7:
  579.                     $mul = 3;
  580.                     break;
  581.                 case 3:
  582.                     $mul = 1;
  583.                     break;
  584.                 case 1:
  585.                     $mul = 7;
  586.                     break;
  587.                 }
  588.             }
  589.             $refSum substr(10 - ($refSum % 10)-1);
  590.             if($refSum == $controlNum){
  591.                 return true;
  592.             }
  593.         }
  594.         return false;
  595.     }
  596.     // }}}
  597.     
  598.     // {{{ bool Validate_FI::creditCard( string $number )
  599.     /**
  600.      * Validate credit card number.
  601.      * 
  602.      * This method checks the credit card number according to Luhn algorithm.
  603.      * This method doesn't guarantee that the card is legitimate.
  604.      * 
  605.      * <code>
  606.      * <?php
  607.      * // Include the package
  608.      * require_once 'Validate/FI.php';
  609.      * 
  610.      * $creditCard = '5427 0073 1297 6425';
  611.      * if ( Validate_FI::creditCard($creditCard) ) {
  612.      *     print 'Valid';
  613.      * } else {
  614.      *     print 'Not valid!';
  615.      * }
  616.      * 
  617.      * ?>
  618.      * </code>
  619.      * 
  620.      * @static
  621.      * @access      public
  622.      * @param       string  $number credit card number to be validated
  623.      *                       (spaces and dashes tolerated)
  624.      * @return      bool    true if credit card number is valid, false otherwise
  625.      */
  626.     function creditCard($number)
  627.     {
  628.         // Remove non-numeric characters from $number 
  629.         $number ereg_replace('[^0-9]+'''$number);
  630.         // Validate number
  631.         return Validate_FI::_mod10($number);
  632.     }
  633.     // }}}
  634.     
  635.     // {{{ bool Validate_FI::_mod10( string $number )
  636.     /**
  637.      * Validate number according to Luhn algorithm (mod 10).
  638.      * 
  639.      * This method checks given number according Luhn algorithm.
  640.      * 
  641.      * @static
  642.      * @access      private
  643.      * @param       string  $number to be validated
  644.      * @return      bool    true if number is valid, false otherwise
  645.      * @link        http://en.wikipedia.org/wiki/Luhn_algorithm
  646.      */
  647.     function _mod10($number)
  648.     {
  649.         // Double every second digit started at the right
  650.         $doubledNumber  '';
  651.         $odd            = false;
  652.         for($i strlen($number)-1; $i >=0; $i--)
  653.         {
  654.             $doubledNumber .= ($odd$number[$i]*2 : $number[$i];
  655.             $odd            !$odd;
  656.         }
  657.         // Add up each 'single' digit
  658.         $sum = 0;
  659.         for($i = 0; $i strlen($doubledNumber)$i++{
  660.             $sum += (int)$doubledNumber[$i];
  661.         }
  662.         // A valid number doesn't have a remainder after mod10 or equal to 0
  663.         return (($sum % 10 == 0&& ($sum != 0)) ? true : false;
  664.     }
  665.     // }}}
  666. }
  667. // }}}
  668.  
  669. ?>

Documentation generated on Thu, 05 Jul 2007 08:00:04 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.