Last active
January 2, 2023 09:03
-
-
Save gbaudoin/9066c1a3d6ab68351635 to your computer and use it in GitHub Desktop.
Calcul du chiffe-clé en PHP, modulo 10 récursif, BVR PostFinance Suisse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Calcul du chiffre-clé en PHP, modulo 10, récursif BVR Suisse | |
* Berechnung der Prüfziffer nach Modulo 10, rekursiv BESR Schweiz | |
* Calculating the Check Digits Using Module 10, Recursively | |
* | |
* @author gbaudoin | |
* @license GPL | |
* @see https://www.postfinance.ch/content/dam/pf/de/doc/consult/manual/dldata/efin_recdescr_man_fr.pdf | |
* @see https://gist.github.com/christianmeichtry/9348451 | |
*// | |
class Bvr { | |
private static $table = [ | |
[0,9,4,6,8,2,7,1,3,5], | |
[9,4,6,8,2,7,1,3,5,0], | |
[4,6,8,2,7,1,3,5,0,9], | |
[6,8,2,7,1,3,5,0,9,4], | |
[8,2,7,1,3,5,0,9,4,6], | |
[2,7,1,3,5,0,9,4,6,8], | |
[7,1,3,5,0,9,4,6,8,2], | |
[1,3,5,0,9,4,6,8,2,7], | |
[3,5,0,9,4,6,8,2,7,1], | |
[5,0,9,4,6,8,2,7,1,3] | |
]; | |
public static function calculate($number) { | |
$report = 0; | |
foreach(str_split($number) as $key => $value) { | |
$report = self::$table[$report][(int)$value]; | |
} | |
return (10 - $report) % 10; | |
} | |
public static function validate($number, $digit) { | |
return $digit == self::calculate($number); | |
} | |
} |
Super cela fonctionne !
il me manquait la syntaxe correcte : $chk_mod=Bvr::calculate('80106900000000000000004593');
Merci, good job !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Normalement Il faut juste faire
Bvr::calculate('80106900000000000000004593')
et cela devrait retourner2
(cela le fait pour moi). Par contre il faut bien envoyé le numéro de référence sans les espaces entre les sections, sinon la fonction retourne4
.