-
-
Save rgarciaj/7e0418478014183e8df469df487452b2 to your computer and use it in GitHub Desktop.
Validar Rut en php
This file contains hidden or 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 | |
| /** | |
| * Comprueba si el rut ingresado es valido | |
| * @param string $rut RUT | |
| * @return boolean | |
| */ | |
| public function valida_rut($rut) | |
| { | |
| if (!preg_match("/^[0-9.]+[-]?+[0-9kK]{1}/", $rut)) { | |
| return false; | |
| } | |
| $rut = rut_limpio($rut); | |
| $dv = rut_dv($rut); | |
| $numero = rut_numero($rut); | |
| $i = 2; | |
| $suma = 0; | |
| foreach (array_reverse(str_split($numero)) as $v) { | |
| if ($i == 8) | |
| $i = 2; | |
| $suma += $v * $i; | |
| ++$i; | |
| } | |
| $dvr = 11 - ($suma % 11); | |
| if ($dvr == 11) | |
| $dvr = 0; | |
| if ($dvr == 10) | |
| $dvr = 'K'; | |
| if ($dvr == strtoupper($dv)) | |
| return true; | |
| else | |
| return false; | |
| } | |
| function rut_limpio($rut) { | |
| $rut = preg_replace('/[\.\-]/i', '', $rut); | |
| return $rut; | |
| } | |
| function rut_numero($rut) { | |
| $rut = rut_limpio($rut); | |
| $numero = substr($rut, 0, strlen($rut) - 1); | |
| return $numero; | |
| } | |
| function rut_dv($rut) { | |
| $rut = rut_limpio($rut); | |
| $dv = substr($rut, -1); | |
| return $dv; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment