Skip to content

Instantly share code, notes, and snippets.

@ricardodbv
Last active March 8, 2022 15:57
Show Gist options
  • Save ricardodbv/3881019 to your computer and use it in GitHub Desktop.
Save ricardodbv/3881019 to your computer and use it in GitHub Desktop.

Revisions

  1. ricardodbv revised this gist Apr 9, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions validar_rut.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    /**
    * Comprueba si el rut ingresado es valido
    *
  2. ricardodbv created this gist Oct 12, 2012.
    35 changes: 35 additions & 0 deletions validar_rut.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /**
    * 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;
    }