Last active
March 8, 2022 15:57
-
-
Save rbarrigav/3881019 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 $rut string | |
* @return true o false | |
*/ | |
function valida_rut($rut) | |
{ | |
$rut = preg_replace('/[^k0-9]/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; | |
} |
Gracias por compartir!
Mil gracias 👍
Gracias (y)
Error en el código: prueben poniendo "asd" y les arrojará un True
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
muchas gracias!!.