cvs: pear /Validate Validate.php

From: Date: Sun, 16 Feb 2003 17:46:42 +0000
Subject: cvs: pear /Validate Validate.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8893@lists.php.net to get a copy of this message
pajoye Sun Feb 16 12:46:42 2003 EDT Modified files: /pear/Validate Validate.php Log: Add isbn check Update comments Index: pear/Validate/Validate.php diff -u pear/Validate/Validate.php:1.24 pear/Validate/Validate.php:1.25 --- pear/Validate/Validate.php:1.24 Sat Jan 4 06:56:28 2003 +++ pear/Validate/Validate.php Sun Feb 16 12:46:41 2003 @@ -18,7 +18,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: Validate.php,v 1.24 2003/01/04 11:56:28 mj Exp $ +// $Id: Validate.php,v 1.25 2003/02/16 17:46:41 pajoye Exp $ // // Methods for common data validations // @@ -42,8 +42,12 @@ class Validate { /** - * @param mixed $decimal The decimal char or false when decimal not allowed - */ + * Validate a number + * + * @param string $number Number to validate + * @param mixed $decimal The decimal char or false when decimal not allowed + * @param int $dec_prec Number of allowed decimals + */ function number($number, $decimal = null, $dec_prec = null, $min = null, $max = null) { if (is_array($number)) { @@ -67,6 +71,13 @@ return true; } + + /** + * Validate a email + * + * @param string $email URL to validate + * @param boolean $domain_check Check or not if the domain exists + */ function email($email, $check_domain = false) { if (is_array($email)) { @@ -90,8 +101,13 @@ } /** - * @param string $format Ex: VAL_NUM . VAL_ALPHA - */ + * Validate a string using the given format 'format' + * + * @param string $string String to validate + * @param string $format Ex: VAL_NUM . VAL_ALPHA (see constants) + * @param int $min_lenght Minimum length of the string + * @param int $max_lenght Maximum length of the string + */ function string($string, $format = null, $min_lenght = 0, $max_lenght = 0) { if (is_array($string)) { @@ -109,6 +125,12 @@ return true; } + /** + * Validate a URL + * + * @param string $url URL to validate + * @param boolean $domain_check Check or not if the domain exists + */ function url($url, $domain_check = false) { if (is_array($url)) { @@ -297,6 +319,61 @@ } } return true; + } + + /** + * Validate a ISBN number + * + * This function checks given number according + * Usage: + * <?php + * require_once 'Validate.php'; + * if (Validate::ISBN($myisbn)) { + * // ISBN is OK + * } + * ?> + * + * @param string $isbn number (only numeric chars will be considered) + * @return bool true if number is valid, otherwise false + * @author Damein Seguy (dams@nexen.net>, + * added by Pierre-Alain Joye <paj@pearfr.org> + */ + function ISBN($isbn) + { + + if (preg_match("/[^0-9 IXSBN-]/", $isbn)) { + return false; + } + + if (!ereg("^ISBN", $isbn)){ + return false; + } + + $isbn = ereg_replace("-", "", $isbn); + $isbn = ereg_replace(" ", "", $isbn); + $isbn = eregi_replace("ISBN", "", $isbn); + if (strlen($isbn) != 10) { + return false; + } + if (preg_match("/[^0-9]{9}[^0-9X]/", $isbn)){ + return false; + } + + $t = 0; + for($i=0; $i< strlen($isbn)-1; $i++){ + $t += $isbn[$i]*(10-$i); + } + $f = $isbn[9]; + if ($f == "X") { + $t += 10; + } else { + $t += $f; + } + if ($t % 11) { + return false; + } else { + return true; + } } function _substr(&$date, $num, $opt = false)

« previous php.pear.cvs (#8893) next »