LEI (Legal Entity Identifier) code is a unique identifier code for legal entities making financial transactions. Which purpose is to help identify legal entities on a globally accessible database. Some times we need an validator for such codes, but as far I know, there is not so much PHP scripts doing this validation. This is why I will provide my code for making LEI code verification in PHP
function validateLEI($input) { $lei = strtolower($input); // Check if the whole string is exactly 20 symbols if(strlen($lei) != 20) return false; // Subsitution scheme for letters $Chars = array( 'a'=>10,'b'=>11,'c'=>12,'d'=>13,'e'=>14,'f'=>15,'g'=>16,'h'=>17,'i'=>18,'j'=>19,'k'=>20,'l'=>21,'m'=>22, 'n'=>23,'o'=>24,'p'=>25,'q'=>26,'r'=>27,'s'=>28,'t'=>29,'u'=>30,'v'=>31,'w'=>32,'x'=>33,'y'=>34,'z'=>35 ); // Replacing every letter with numbers $NewString = strtr($lei, $Chars); // Now we just need to validate the checksum // Use bcmod if available if (function_exists("bcmod")) { return bcmod($NewString, '97') == 1; } // Else use this workaround // http://au2.php.net/manual/en/function.bcmod.php#38474 $x = $NewString; $y = "97"; $take = 5; $mod = ""; do { $a = (int)$mod . substr($x, 0, $take); $x = substr($x, $take); $mod = $a % $y; } while (strlen($x)); return (int)$mod == 1; }
This was the function that you have to declare, and then just use:
// Checking sample LEI number var_dump(validateLEI("ZNHTL6O2WODTKMPF1Z48"));
If the result is TRUE – then the Lei code is valid, if it’s FALSE… it’s not 🙂
Enjoy!