-
-
Save punchi/3a5c44e7aa7ac0609ce9e53365572541 to your computer and use it in GitHub Desktop.
Validar Rut en php
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 | |
/** | |
* 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 = preg_replace('/[\.\-]/i', '', $rut); | |
$dv = substr($rut, -1); | |
$numero = substr($rut, 0, strlen($rut) - 1); | |
$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; | |
} |
@lucasschirm is valid rut 123123123 = 12.312.312-3
Hola! alguna forma de validar correctamente solo si incluyen el guion? (para obligar el uso del guion), saludos!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is considering 123123123 as valid, should i do more validation befor pass to this?