= 0; $i -= 2) { // Add up every 2nd digit, starting from the right $checksum += substr($number, $i, 1); } for ($i = $length - 2; $i >= 0; $i -= 2) { // Add up every 2nd digit doubled, starting from the right $double = substr($number, $i, 1) * 2; // Subtract 9 from the double where value is greater than 10 $checksum += ($double >= 10) ? $double - 9 : $double; } // If the checksum is a multiple of 10, the number is valid return ($checksum % 10 === 0); } /** * Checks if a phone number is valid. * * @param string phone number to check * @return boolean */ public static function phone($number, $lengths = NULL) { if ( ! is_array($lengths)) { $lengths = array(7,10,11); } // Remove all non-digit characters from the number $number = preg_replace('/\D+/', '', $number); // Check if the number is within range return in_array(strlen($number), $lengths); } /** * Checks whether a string consists of alphabetical characters only. * * @param string input string * @param boolean trigger UTF-8 compatibility * @return boolean */ public static function alpha($str, $utf8 = FALSE) { return ($utf8 === TRUE) ? (bool) preg_match('/^\pL++$/uD', (string) $str) : ctype_alpha((string) $str); } /** * Checks whether a string consists of alphabetical characters and numbers only. * * @param string input string * @param boolean trigger UTF-8 compatibility * @return boolean */ public static function alpha_numeric($str, $utf8 = FALSE) { return ($utf8 === TRUE) ? (bool) preg_match('/^[\pL\pN]++$/uD', (string) $str) : ctype_alnum((string) $str); } /** * Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only. * * @param string input string * @param boolean trigger UTF-8 compatibility * @return boolean */ public static function alpha_dash($str, $utf8 = FALSE) { return ($utf8 === TRUE) ? (bool) preg_match('/^[-\pL\pN_]++$/uD', (string) $str) : (bool) preg_match('/^[-a-z0-9_]++$/iD', (string) $str); } /** * Checks whether a string consists of digits only (no dots or dashes). * * @param string input string * @param boolean trigger UTF-8 compatibility * @return boolean */ public static function digit($str, $utf8 = FALSE) { return ($utf8 === TRUE) ? (bool) preg_match('/^\pN++$/uD', (string) $str) : ctype_digit((string) $str); } /** * Checks whether a string is a valid number (negative and decimal numbers allowed). * * @param string input string * @return boolean */ public static function numeric($str) { return (is_numeric($str) AND preg_match('/^[-0-9.]++$/D', (string) $str)); } /** * Checks whether a string is a valid text. Letters, numbers, whitespace, * dashes, periods, and underscores are allowed. * * @param string $str * @return boolean */ public static function standard_text($str) { return (bool) preg_match('/^[-\pL\pN\pZ_.]++$/uD', (string) $str); } /** * Checks if a string is a proper decimal format. The format array can be * used to specify a decimal length, or a number and decimal length, eg: * array(2) would force the number to have 2 decimal places, array(4,2) * would force the number to have 4 digits and 2 decimal places. * * @param string input string * @param array decimal format: y or x,y * @return boolean */ public static function decimal($str, $format = NULL) { // Create the pattern $pattern = '/^[0-9]%s\.[0-9]%s$/'; if ( ! empty($format)) { if (count($format) > 1) { // Use the format for number and decimal length $pattern = sprintf($pattern, '{'.$format[0].'}', '{'.$format[1].'}'); } elseif (count($format) > 0) { // Use the format as decimal length $pattern = sprintf($pattern, '+', '{'.$format[0].'}'); } } else { // No format $pattern = sprintf($pattern, '+', '+'); } return (bool) preg_match($pattern, (string) $str); } } // End valid