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

Source for file ISIN.php

Documentation is available at ISIN.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * Validation function for ISINs
  6.  * (International Securities Identification Numbers)
  7.  *
  8.  * PHP versions 4
  9.  *
  10.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  11.  * that is available through the world-wide-web at the following URI:
  12.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  13.  * the PHP License and are unable to obtain it through the web, please
  14.  * send a note to license@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category   Validate
  17.  * @package    Validate_Finance_ISIN
  18.  * @author     Stephan Jakoubek <stephan-pear@jakoubek.de>
  19.  * @author     Uli Honal <uli@netzgeist.de>
  20.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  21.  */
  22.  
  23. {
  24.     /**
  25.      * Validate an ISIN (International Securities Identification Number, ISO 6166)
  26.      *
  27.      * @param     string      $isin              ISIN to be validated
  28.      * @access    public
  29.      * @return    boolean   true if ISIN is valid
  30.      */
  31.     function validate($isin)
  32.     {
  33.         // Formal check.
  34.         if (!preg_match('/^[A-Z]{2}[A-Z0-9]{9}[0-9]$/i'$isin)) {
  35.             return false;
  36.         }
  37.  
  38.         // Convert letters to numbers.
  39.         $base10 '';
  40.         for ($i = 0; $i <= 11; $i++{
  41.             $base10 .= base_convert($isin{$i}3610);
  42.         }
  43.  
  44.         // Calculate double-add-double checksum.
  45.         $checksum = 0;
  46.         $len strlen($base10- 1;
  47.         $parity $len % 2;
  48.         // Iterate over every digit, starting with the rightmost (=check digit).
  49.         for ($i $len$i >= 0; $i--{
  50.             // Multiply every other digit by two.
  51.             $weighted $base10{$i<< (($i $parity1);
  52.             // Sum up the weighted digit's digit sum.
  53.             $checksum += $weighted % 10 + (int)($weighted / 10);
  54.         }
  55.  
  56.         return !(bool)($checksum % 10);
  57.     // end func Validate_Finance_ISIN
  58.  
  59. // end class Validate_Finance_ISIN
  60.  
  61. ?>

Documentation generated on Mon, 11 Mar 2019 14:57:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.