Skip to content

Instantly share code, notes, and snippets.

@rbarrigav
Last active March 8, 2022 15:57
Show Gist options
  • Save rbarrigav/3881019 to your computer and use it in GitHub Desktop.
Save rbarrigav/3881019 to your computer and use it in GitHub Desktop.
Validar Rut en php
<?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;
}
@quenox
Copy link

quenox commented Mar 26, 2021

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