Re: cvs: pear /Validate Validate.php

From: Date: Wed, 29 May 2002 08:29:55 +0000
Subject: Re: cvs: pear /Validate Validate.php
References: 1  Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4235@lists.php.net to get a copy of this message
Bertrand Mansion wrote: > > le 29/05/02 9:41, Tomas V.V.Cox à cox@idecnet.com a écrit : > > > cox Wed May 29 03:41:58 2002 EDT > > > > Modified files: > > /pear/Validate Validate.php > > Log: > > - Enhance number() with decimal precition and range > > - Added url validation > > > > > > Index: pear/Validate/Validate.php > > diff -u pear/Validate/Validate.php:1.1 pear/Validate/Validate.php:1.2 > > --- pear/Validate/Validate.php:1.1 Tue May 28 07:33:49 2002 > > +++ pear/Validate/Validate.php Wed May 29 03:41:57 2002 > > @@ -1,6 +1,7 @@ > > <?php > > - > > -die('Unfinished, untested, unworking. Don\'t use it'); > > +/** > > +Experimental > > +*/ > > > > define('VAL_NUM', '0-9'); > > define('VAL_SPACE', '\s'); > > @@ -19,10 +20,24 @@ > > /** > > * @param mixed $decimal The decimal char or false when decimal not allowed > > */ > > - function number($number, $decimal = false) > > + function number($number, $decimal = null, $dec_prec = null, $min = null, > > $max = null) > > { > > - $decimal = $decimal ? $decimal . '[0-9]+' : ''; > > - return ereg("^[0-9]+$decimal\$", $number); > > + $dec_prec = $dec_prec ? "{0,$dec_prec}" : '*'; > > + $dec_regex = $decimal ? $decimal . '[0-9]' . $dec_prec : > > ''; > > + if (!ereg("^[0-9]+($dec_regex)?\$", $number)) { > > + return false; > > + } > > + if ($decimal != '.') { > > + $number = str_replace($decimal, '.', $number); > > + } > > + $number = (float)$number; > > + if ($min !== null && $min > $number) { > > + return false; > > + } > > + if ($max !== null && $max < $number) { > > + return false; > > + } > > + return true; > > } > > > > function email($email, $check_domain = false) > > @@ -51,10 +66,10 @@ > > if ($format && !preg_match("|^[$format]*\$|s", $string)) { > > return false; > > } > > - if ($min_lenght && strlen($string) <= $min_lenght) { > > + if ($min_lenght && strlen($string) < $min_lenght) { > > return false; > > } > > - if ($max_lenght && strlen($string) >= $max_lenght) { > > + if ($max_lenght && strlen($string) > $max_lenght) { > > return false; > > } > > return true; > > @@ -66,6 +81,22 @@ > > trigger_error(' no date class found ..', E_USER_ERROR); > > return false; > > } > > + } > > + > > + function url($url, $domain_check = false) > > + { > > + $purl = parse_url($url); > > + if (eregi('^http$', @$purl['scheme']) && > > !empty($purl['host']) { > > + if ($domain_check && function_exists('checkdnsrr')) { > > + if (checkdnsrr($purl['host'], 'A')) { > > + return true; > > + } else { > > + return false; > > + } > > + } > > + return true; > > + } > > + return false; > > } > > > > function multiple($data, $options) > > Aren't preg faster then ereg ? You are right (haven't benchmarked it before): <?php $a="http://foo.bar"; $t = time(); for ($i=0; $i<1000000;$i++) eregi('^http$', $a); echo "eregi: " . (time() - $t) . "s\n"; $t = time(); for ($i=0; $i<1000000;$i++) preg_match('|^http$|i', $a); echo "preg: " . (time() - $t) . "s\n"; ?> Output: eregi: 12s preg: 5s Of course the difference taken on a single operation is almost null, but commited anyways. > Wouldn't it be better if you split the number method into integer and float > with both minus numbers accepted ? > Do you think is really necessary? - integer validation: Validate::number($number) - float validation: Validate::number($number, '.', 2) // (number, decimal char, precition) If you want to add range validation (let's say between 8 and 12) just: - integer validation: Validate::number($number, null, null, 8, 12) - float validation: Validate::number($number, '.', 2, 8, 12) Tomas V.V.Cox

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