0) { $correctDigit = 10 - ($lastDigitTotalSum % 10); } else { $correctDigit = 0; } } /* If CIF number starts with P, Q, S, N, W or R, check digit sould be a letter */ if (preg_match('/[PQSNWR]/', $firstChar)) { $correctDigit = substr("JABCDEFGHI", $correctDigit, 1); } return $correctDigit; } /* * This function validates the format of a given string in order to * see if it fits a regexp pattern. * * This function is intended to work with Spanish identification * numbers, so it always checks string length (should be 9) and * accepts the absence of leading zeros. * * This function is used by: * - isValidNIFFormat * - isValidNIEFormat * - isValidCIFFormat * * This function returns: * TRUE: If specified string respects the pattern * FALSE: Otherwise * * Usage: * echo respectsDocPattern( * '33576428Q', * '/^[KLM0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z]/' ); * Returns: * TRUE */ function respectsDocPattern($givenString, $pattern) { $isValid = FALSE; $fixedString = strtoupper($givenString); if (is_int(substr($fixedString, 0, 1))) { $fixedString = substr("000000000".$givenString, -9); } if (preg_match($pattern, $fixedString)) { $isValid = TRUE; } return $isValid; } /* * This function performs the sum, one by one, of the digits * in a given quantity. * * For instance, it returns 6 for 123 (as it sums 1 + 2 + 3). * * This function is used by: * - getCIFCheckDigit * * Usage: * echo sumDigits( 12345 ); * Returns: * 15 */ function sumDigits($digits) { $total = 0; $i = 1; while ($i <= strlen($digits)) { $thisNumber = substr($digits, $i - 1, 1); $total += $thisNumber; $i++; } return $total; } ?>