Return to Snippet

Revision: 586
at July 20, 2006 06:43 by nicolaspar


Initial Code
function rutValido( $rut ){
	if( empty( $rut ) ) return false;

	if (!preg_match("/(\d{7,8})-([\dK])/", strtoupper($rut), $aMatch)) { 
	 return false; 
	} 
	$sRutBase = substr(strrev($aMatch[1]) , 0, 8 ); 
	$sCodigoVerificador = $aMatch[2]; 
	$iCont = 2; 
	$iSuma = 0; 
	for ($i = 0;$i<strlen($sRutBase);$i++) { 
	 if ($iCont>7) { 
		 $iCont = 2; 
	 } 
	 $iSuma+= ($sRutBase{$i}) *$iCont; 
	 $iCont++; 
	} 
	$iDigito = 11-($iSuma%11); 
	$sCaracter = substr("-123456789K0", $iDigito, 1); 
	return ($sCaracter == $sCodigoVerificador);
} 
echo rutValido('12159116-2') ? 1 : 0; #Out 1
echo rutValido('54545454545') ? 1 : 0; #Out 0

Initial URL

                                

Initial Description

                                

Initial Title
Php validar Rut (Documento de identidad chileno)

Initial Tags

                                

Initial Language
PHP