Calcula el dígito verificador de un rut

El rut puede ser ingresado en los siguientes formatos:

* xx.xxx.xxx (con separación de miles)
* xxxxxxxx (sin separación de miles)

@access		public

@param		string $rut
@return		string|FALSE
19298
public function digitoVerificador($rut) {

		//Preparamos el RUT recibido
		$numero = $this->clean($rut, false);

		//Calculamos el dígito verificador
		$txt		= array_reverse(str_split($numero));
		$sum		= 0;
		$factors	= array(2,3,4,5,6,7,2,3,4,5,6,7);

		foreach($txt as $key => $chr) {
			$sum += $chr * $factors[$key];
		}
		
		$a			= $sum % 11;
		$b			= 11-$a;
		
		if($b == 11)
			$digitoVerificador	= 0;
		elseif($b == 10)
			$digitoVerificador	= 'K';
		else
			$digitoVerificador = $b;

		//Convertimos el número a cadena para efectos de poder comparar
		$digitoVerificador = (string)$digitoVerificador;

		return $digitoVerificador;
	}
1121192
function _divide_digit($dividend, $divisor)
    {
        $carry = 0;
        $result = array();

        for ($i = count($dividend) - 1; $i >= 0; --$i) {
            $temp = MATH_BIGINTEGER_BASE_FULL * $carry + $dividend[$i];
            $result[$i] = $this->_safe_divide($temp, $divisor);
            $carry = (int) ($temp - $divisor * $result[$i]);
        }

        return array($result, $carry);
    }
140556
public static function validate(...$ruts)
    {
        if (is_array($ruts[0]) && func_num_args() === 1) {
            $ruts = $ruts[0];
        }

        return self::performValidate($ruts);
    }
122960
public function toFloat($num)
	{
		$dotPos = strrpos($num, '.');
		$commaPos = strrpos($num, ',');
		$sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);

		if (!$sep) {
			return floatval(preg_replace('/[^0-9]/', '', $num));
		}

		return floatval(
			preg_replace('/[^0-9]/', '', substr($num, 0, $sep)) . '.' .
			preg_replace('/[^0-9]/', '', substr($num, $sep+1, strlen($num)))
		);
	}
1186382
public function numberFormat($decimals = 0, $thousandSeparator = '.', $decimalSeparator = ',')
    {
        $decimalSeparator = $thousandSeparator === '.' ? ',' : '.';

        return number_format($this->amount, $decimals, $decimalSeparator, $thousandSeparator);
    }
836431
public function format($format)
    {
        // Sprintf and unit regex
        $pattern = '/(%(?:\d+\$)?[+-]?(?:[ 0]|\'.{1})?-?\d*(?:\.\d+)?[bcdeEufFgGosxX])(.*)/';

        // Check if string has sprintf and unit...
        if (preg_match_all($pattern, $format, $m)) {
            // Valid format, save to instance.
            $this->format = $format;

            // Get sprintf and units
            $sprintf = $m[1][0];
            $unit    = $m[2][0];

            // Try to find value of instance at specified units.
            $value = $this->{trim($unit)};
            if ($value == 0) {
                $value = $this->{strtolower(trim($unit))};
                if ($value == 0) {
                    $value = $this->{strtoupper(trim($unit))};
                }
            }

            // Should just display as scientific notation?
            if (sprintf($sprintf, $value) == 0 || $value > 10e9) {
                // Change sprintf to use e for scientific notation
                $sprintf = preg_replace('/[bcdeEufFgGosxX]/', 'e', $sprintf);
            }

            // Return value with format and unit suffix.
            return sprintf($sprintf, $value) . $unit;
        } else {
            // No valid format found, use old format.
            if (preg_match_all($pattern, $this->format, $m)) {
                return $this->format(
                  str_replace(trim($m[2][0]), '', $m[0][0]) . $format);
            }
        }
    }
252644
public static function ordinal($number)
    {
        $test_c = abs($number) % 10;
        $ext = ((abs($number) % 100 < 21 && abs($number) % 100 > 4) ? 'th' : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th'));

        return $number . $ext;
    }
610580
function _divide_digit($dividend, $divisor)
    {
        $carry = 0;
        $result = array();

        for ($i = count($dividend) - 1; $i >= 0; --$i) {
            $temp = self::$baseFull * $carry + $dividend[$i];
            $result[$i] = $this->_safe_divide($temp, $divisor);
            $carry = (int) ($temp - $divisor * $result[$i]);
        }

        return array($result, $carry);
    }
1309916
public function toCurrencyWords($currency, $decimal, $fraction = null)
    {
        $negative = 0;
        $ret = [];
        $noDecimals = false;

        /**
         * Negative ?
         * We can lose the '-' sign if we do the
         * check after number_format() call (i.e. -0.01)
         */
        if (substr($decimal, 0, 1) == '-') {
            $decimal = -$decimal;
            $negative = 1;
        }

        /**
         * Removes leading zeros, spaces, decimals etc.
         * Adds thousands separator.
         */
        $num = number_format($decimal, 0, '', '');

        /**
         * Checking if given currency exists in driver.
         * If not, use default currency
         */
        $currency = strtoupper($currency);
        if (!isset(self::$currencyNames[$currency])) {
            throw new NumberToWordsException(
                sprintf('Currency "%s" is not available for "%s" language', $currency, get_class($this))
            );
        }

        /**
         * Currency names and plural
         */
        $currencyNames = self::$currencyNames[$currency];

        if ($num > 0) {
            /**
             * Word representation of decimal
             */
            $ret[] = $this->toWords($num);

            /**
             * Special case.
             */
            if (substr($num, -6) == '000000') {
                $ret[] = trim($this->currencySeparator);
            }

            /**
             * Testing plural. Adding currency name
             */
            if ($num > 1) {
                $ret[] = $currencyNames[0][1];
            } else {
                $ret[] = $currencyNames[0][0];
            }
        }

        /**
         * Test if fraction was given and != 0
         */
        $fraction = (int) $fraction;
        if ($fraction) {

            /**
             * Removes leading zeros, spaces, decimals etc.
             * Adds thousands separator.
             */
            $num = number_format($fraction, 0, '.', '.');

            if ($num < 0 || $num > 99) {
                throw new NumberToWordsException('Fraction out of range.');
            }

            if (count($ret)) {
                $ret[] = trim($this->separator);
            } else {
                $noDecimals = true;
            }


            $ret[] = $this->toWords($num);

            if ($num > 1) {
                $ret[] = $currencyNames[1][1];
            } else {
                $ret[] = $currencyNames[1][0];
            }

            if ($noDecimals) {
                $ret[] = trim($this->currencySeparator);
                $ret[] = $currencyNames[0][0];
            }
        }

        if ($negative) {
            $ret[] = $this->minus;
        }

        return implode(' ', $ret);
    }
228126
public function parseDecimal()
    {
        $scale = $this->parseUnsignedInt8();
        $value = $this->parseSignedInt32();

        if (0 === $scale) {
            return (string)$value;
        }

        if ($value >= 0) {
            $sign = '';
            $value = (string)$value;
        } else {
            $sign = '-';
            $value = (string)-$value;
        }

        $length = strlen($value);

        if ($length === $scale) {
            return $sign . '0.' . $value;
        }

        if ($length < $scale) {
            return $sign . '0.' . str_repeat('0', $scale - $length) . $value;
        }

        return $sign . substr($value, 0, -$scale) . '.' . substr($value, -$scale);
    }
1250999
public static function getCheckDigit(string $number) : int
    {
        if (! ctype_digit($number)) {
            throw new \InvalidArgumentException('The number must be a string of digits');
        }

        $checksum = self::checksum($number . '0');

        return ($checksum === 0) ? 0 : 10 - $checksum;
    }
739589
public static function encodeHex($dec)
    {
        if (!is_string($dec) && !ctype_digit($dec)) {
            throw new \Exception(sprintf('Argument is expected to be a string of decimal numbers. You passed in "%s"', gettype($dec)));
        }

        if (substr($dec, 0, 1) === '-') {
            $dec = substr($dec, 1);
        }

        $hex = '';

        while (Math::cmp($dec, 0) > 0) {
            $q = Math::div($dec, 16);
            $rem = Math::mod($dec, 16);
            $dec = $q;

            $hex = substr(self::HEX_CHARS, intval($rem), 1).$hex;
        }

        return $hex;
    }
957395
public static function convert($number)
    {
        $number = preg_replace('/\,/i', '', $number);

        $number = preg_replace('/([^0-9\.\-])/i', '', $number);

        if (! is_numeric($number)) {
            return '0.00';
        }

        $isCents = (bool) preg_match('/^0.\d+$/', $number);

        return ($isCents ? '0' : null).number_format($number * 100., 0, '.', '');
    }
1319513
public static function normalizeLat(float $lat): float
    {
        if ($lat > 360.0) {
            $lat = \fmod($lat, 360.0);
        }

        if ($lat < -360.0) {
            $lat = \fmod($lat, -360.0);
        }

        if ($lat > 180.0) {
            $lat = 180.0 - $lat;
        }

        if ($lat < -180.0) {
            $lat = -180.0 - $lat;
        }

        if ($lat > 90.0) {
            $lat = 180.0 - $lat;
        }

        if ($lat < -90.0) {
            $lat = -180.0 - $lat;
        }

        return $lat;
    }
411188
public function str($flags = 0)
    {
        $ret = false;

        if ($this->match(TextTokenizer::STRING, $matches, $flags)) {
            $delimiter = $matches[2];
            $str = $matches[3];
            $str = str_replace("\\$delimiter", "$delimiter", $str);
            $ret = array($str);
        }

        return $ret;
    }
998857
public static function separateRut(string $rut, bool $uppercase = true)
    {
        // Throw an exception if after cleaning the RUT we receive null, since
        // we cannot separate an empty string, thus making the resulting
        // array impossible to return. Also, it makes it catchable.
        if (empty($cleaned = self::cleanRut($rut, $uppercase))) {
            throw new InvalidRutException($rut);
        }

        $array = self::explodeByLastChar($cleaned);

        $array[0] = (int)$array[0];

        return $array;
    }
1179534
public static function toNumbers($s){
		$ret = array();
		for($i=0; $i<strlen($s); $i+=2){
			$ret[] = hexdec(substr($s, $i, 2));
		}
		return $ret;
	}