Jim,

Thanks for sharing all your hard work. Have you seen checkdate()?
http://www.php.net/manual/en/function.checkdate.php

Kirk

> -----Original Message-----
> From: Ide, Jim [mailto:[EMAIL PROTECTED]]
> Sent: Monday, March 19, 2001 6:38 AM
> To: 'phpman'; [EMAIL PROTECTED]
> Subject: RE: [PHP] how do i get a variable type? - not that simple
> 
> 
> These are the functions I use to determine if
> a string value is a valid integer, real, date, etc.
> Hope this helps.
> 
> ps - please let me know if you find any errors.  thanx.
> 
> <?php
> 
> function IsValidBoolean($p) {
>       if ( isset($p) ) {
>               if ((strtolower($p) == "true") or (strtolower($p) ==
> "false"))             return TRUE;
>       }
>       return FALSE;
> }
> 
> function IsValidMemo($p) {
> 
>       // If $p contains any characters other than printable ascii,
>       // or \r or \n, return FALSE.
> 
>       if (trim($p) == "")                     return TRUE;
>       if ( ereg("[^\r\n\x20-\x7E]", $p) )     return FALSE;
>       return TRUE;
> }
> 
> function IsValidString($p) {
> 
>       // If $p contains any characters other than printable ascii,
>       // return FALSE.
> 
>       if (trim($p) == "")                     return TRUE;
>       if ( ereg("[^\x20-\x7E]", $p) )         return FALSE;
>       return TRUE;
> }
> 
> function IsValidInteger($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p consists of 1 or more digits, optionally preceded by a
> minus sign
> 
>       if ( isset($p) )        return ereg("^\-?[0-9]+$",$p);
>       return FALSE;
> }
> 
> function IsValidReal($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p begins with optional minus sign, followed by 1 or more
> digits, followed by a decimal point,
>       //    followed by 1 or more digits
>       // 3. $p can be optionally preceded by a minus sign
> 
>       if ( isset($p) )        return ereg("^\-?[0-9]+\.[0-9]+$",$p);
>       return FALSE;
> }
> 
> function IsLeapYear($y) {
>       if ( (($y % 4) == 0) && (($y % 100) != 0) || (($y % 400) == 0))
> return TRUE;
>       return FALSE;
> }
> 
> function IsValidDate($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p consists of 4 digits followed by minus sign, 
> followed by 2
> digits, followed by
>       //    minus sign, followed by 2 digits
>       // example 1995-02-03
> 
>       if ( ! isset($p) )
> return FALSE;
>       if ( ! ereg("^([0-9]{4})\-([0-9]{2})\-([0-9]{2})$",$p) )
> return FALSE;
> 
>       $t = explode("-",$p);
>       if ( count($t) != 3 )   return FALSE;
>       $y = $t[0];
>       $m = $t[1];
>       $d = $t[2];
> 
>       return IsValidDate2($y,$m,$d);
> }
> 
> function IsValidDate2($y,$m,$d) {
> 
>       if ( ($m < 1) or ($m > 12) )            return FALSE;
>       if ( ($d < 1) or ($d > 31) )            return FALSE;
> 
>       // 30 days has sept, apr, jun, and nov (all the rest 
> have 31 [except
> for feb])
>       //               9    4    6        11
> 
>       if ( ($m == 4) or ($m == 6) or ($m == 9) or ($m == 11) ) {
>               if ($d > 30)    return FALSE;
>       } elseif ($m == 2) {
>               if ( IsLeapYear($y) ) {
>                       if ( $d > 29 )  return FALSE;
>               } else {
>                       if ( $d > 28 )  return FALSE;
>               }
>       }
>       return TRUE;
> }
> 
> function IsValidTime($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p consists of 2 digits in the range 00 to 23,
>       //      followed by a colon,
>       //      followed by 2 digits in the range 00 to 59,
>       //      followed by a colon,
>       //      followed by 2 digits in the range 00 to 59.
>       // example 14:45:02
> 
>       if ( ! isset($p) )
> return FALSE;
>       if ( ! ereg("^([0-9]{2})\:([0-9]{2})\:([0-9]{2})$",$p) )
> return FALSE;
> 
>       $t = explode(":",$p);
>       if ( count($t) != 3 )   return FALSE;
>       $h = $t[0];
>       $m = $t[1];
>       $s = $t[2];
> 
>       return IsValidTime2($h,$m,$s);
> }
> 
> function IsValidTime2($h,$m,$s) {
> 
>       if ( ($h < 0) or ($h > 23) )    return FALSE;
>       if ( ($m < 0) or ($m > 59) )    return FALSE;
>       if ( ($s < 0) or ($s > 59) )    return FALSE;
> 
>       return TRUE;
> }
> 
> function IsValidDateTime($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p consists of a valid date (validated by 
> IsValidDate() above)
>       //      followed by a space
>       //      followed by a valid time (validated by 
> IsValidTime() above)
>       // example: 1998-04-21 22:01:34
> 
>       if ( ! isset($p) )              return FALSE;
> 
>       $temp = explode(" ",$p);
>       if ( count($temp) != 2 )        return FALSE;
>       $d = $temp[0];
>       $t = $temp[1];
>       if ( ! IsValidDate($d) )        return FALSE;
>       if ( ! IsValidTime($t) )        return FALSE;
> 
>       return TRUE;
> }
> 
> function IsValidTimeStamp($p) {
> 
>       // return TRUE if:
>       // 1. $p is not equal to ""
>       // 2. $p consists of a datetime in YYYYMMDDHHMMSS format
>       // example: 19980421220134
> 
>       if ( ! isset($p) )                      return FALSE;
>       if ( ! ereg("^([0-9]{14})$",$p) )       return FALSE;
> 
>       $y = substr($p,0,4);
>       $m = substr($p,4,2);
>       $d = substr($p,6,2);
>       $h = substr($p,8,2);
>       $min = substr($p,10,2);
>       $s = substr($p,12,2);
> 
>       if ( ! IsValidDate2($y,$m,$d) )         return FALSE;
>       if ( ! IsValidTime2($h,$min,$s) )       return FALSE;
> 
>       return TRUE;
> }
> 
> function TestVF($type,$fn,$v,$p) {
> 
>       // TestVF = TestValidationFunction
> 
>       // example 1:   
> TestVF("integer","IsValidInteger",TRUE,"15"); //
> ok, 15 is a valid integer, so print nothing
>       // example 2:   
> TestVF("integer","IsValidInteger",FALSE,"15");        //
> error, 15 *is* a valid integer, so print error
>       // example 3:   
> TestVF("integer","IsValidInteger",FALSE,"abc");       //
> ok, "abc" is NOT a valid integer, so print nothing
>       // example 4:   
> TestVF("integer","IsValidInteger",TRUE,"abc");        //
> error, "abc" is NOT a valid integer, so print error
> 
>       $IsValid = $fn($p);
>       if ( $v ) {
>               if ( $IsValid )         return;
>               echo "error, is NOT a valid $type: " . $p . "<BR>\n";
>       } else {
>               if ( ! $IsValid )       return;
>               echo "error, *is* a valid $type: " . $p . "<BR>\n";
>       }
> }
> 
> function TestDatesNumbers() {
> 
>       echo "The test is sucessful if nothing else gets printed below
> this.<BR>\n";
> 
>       // x1F = cc
>       // x20 = space
>       // x21 = !
>       // ...
>       // x7D = }
>       // x7E = ~
>       // x7F = cc
> 
>       TestVF("string","IsValidString",TRUE,   "");
>       TestVF("string","IsValidString",TRUE,   "asdfasdf");
>       TestVF("string","IsValidString",FALSE,  "asdf\nasdf");
> // contains \n
>       TestVF("string","IsValidString",FALSE,  "abcd" . chr(0) 
> . "asdf");
> // contains chr(0)
>       TestVF("string","IsValidString",FALSE,  "abcd\x1Fasdf");
> // contains cc
>       TestVF("string","IsValidString",TRUE,   "abcd asdf");
>       TestVF("string","IsValidString",TRUE,   "abcd~asdf");
>       TestVF("string","IsValidString",FALSE,  "abcd\x7Fasdf");
> // contains cc
> 
>       TestVF("memo","IsValidMemo",TRUE,       "");
>       TestVF("memo","IsValidMemo",TRUE,       "asdfasdf");
>       TestVF("memo","IsValidMemo",TRUE,       "asdf\nasdf");
>       TestVF("memo","IsValidMemo",TRUE,       "asdf\rasdf");
>       TestVF("memo","IsValidMemo",TRUE,       "asdf\r\nnasdf");
>       TestVF("memo","IsValidMemo",FALSE,      "abcd" . chr(0) 
> . "asdf");
> // contains chr(0)
>       TestVF("memo","IsValidMemo",FALSE,      "abcd\x1Fasdf");
> // contains cc
>       TestVF("memo","IsValidMemo",TRUE,       "abcd asdf");
>       TestVF("memo","IsValidMemo",TRUE,       "abcd~asdf");
>       TestVF("memo","IsValidMemo",FALSE,      "abcd\x7Fasdf");
> // contains cc
> 
>       // digits    / 0-9 :
> 
>       TestVF("integer","IsValidInteger",FALSE,        "");    
>       //
> blank
>       TestVF("integer","IsValidInteger",TRUE,         "0");
>       TestVF("integer","IsValidInteger",FALSE,        " 0");  
>       //
> leading space not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "+0");  
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",TRUE,         "-0");
>       TestVF("integer","IsValidInteger",FALSE,        "0+");  
>       //
> trailing plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "0-");  
>       //
> trailing minus sign not allowed
>       TestVF("integer","IsValidInteger",TRUE,         "1");
>       TestVF("integer","IsValidInteger",FALSE,        "+1");  
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",TRUE,         "-1");
>       TestVF("integer","IsValidInteger",FALSE,        "1+");  
>       //
> trailing plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "1-");  
>       //
> trailing minus sign not allowed
>       TestVF("integer","IsValidInteger",TRUE,         "11");
>       TestVF("integer","IsValidInteger",FALSE,        "+11"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",TRUE,         "-11");
>       TestVF("integer","IsValidInteger",FALSE,        "11+"); 
>       //
> trailing plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "11-"); 
>       //
> trailing minus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "+-1"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "-+1"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "1+-"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "1-+"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "1+1"); 
>       //
> plus sign not allowed
>       TestVF("integer","IsValidInteger",FALSE,        "1-1"); 
>       //
> minus sign must be leftmost character
>       TestVF("integer","IsValidInteger",FALSE,        "1o1"); 
>       // o
> not allowed
>       TestVF("integer","IsValidInteger",TRUE,
> "243095230982340982360923460972362356236");
>       TestVF("integer","IsValidInteger",TRUE,
> "-243095230982340982360923460972362356236");
>       TestVF("integer","IsValidInteger",FALSE,        "
> -243095230982340982360923460972362356236");   // leading 
> space not allowed
>       TestVF("integer","IsValidInteger",FALSE,        
> "3214532.43");        //
> decimal point not allowed in an integer
>       TestVF("integer","IsValidInteger",FALSE,        "/32"); 
>       //
> contains /
>       TestVF("integer","IsValidInteger",TRUE,         "0");
>       TestVF("integer","IsValidInteger",TRUE,         "9");
>       TestVF("integer","IsValidInteger",FALSE,        ":");   
>       //
> contains :
> 
>       TestVF("real","IsValidReal",FALSE,      "");            // blank
>       TestVF("real","IsValidReal",FALSE,      "0");           // no
> decimal point
>       TestVF("real","IsValidReal",FALSE,      "1");           // no
> decimal point
>       TestVF("real","IsValidReal",TRUE,       "1.1");
>       TestVF("real","IsValidReal",FALSE,      "1.");          
> // no digit
> to the right of decimal point
>       TestVF("real","IsValidReal",FALSE,      ".1");          
> // no digit
> to the left of decimal point
>       TestVF("real","IsValidReal",FALSE,      "-1");          // no
> decimal point
>       TestVF("real","IsValidReal",TRUE,       "-1.1");
>       TestVF("real","IsValidReal",FALSE,      " -1.1");       
> // leading
> space not allowed
> 
>       TestVF("date","IsValidDate",FALSE,      "");            // blank
>       TestVF("date","IsValidDate",TRUE,       "2000-05-24");
>       TestVF("date","IsValidDate",FALSE,      "sdghsdh");     
> // contains
> alpha characters
>       TestVF("date","IsValidDate",FALSE,      "4/5/98");      
> // contains
> slashes
>       TestVF("date","IsValidDate",FALSE,      "2000-9-7");    // not
> YYYY-MM-DD format
>       TestVF("date","IsValidDate",TRUE,       "2000-09-07");
>       TestVF("date","IsValidDate",FALSE,      "2000-09-31");  
> // 30 days
> has september
>       TestVF("date","IsValidDate",FALSE,      "2000-09-00");  
> // day must
> be 01 thru 31
>       TestVF("date","IsValidDate",FALSE,      "2000-19-07");  // month
> must be 01 thru 12
>       TestVF("date","IsValidDate",FALSE,      "2000-00-00");  
> // non-valid
> month and day
>       TestVF("date","IsValidDate",FALSE,      "2000-01-32");  
> // day must
> be 01 thru 31
>       TestVF("date","IsValidDate",TRUE,       "2000-02-28");
>       TestVF("date","IsValidDate",TRUE,       "2000-02-29");
>       TestVF("date","IsValidDate",FALSE,      "2000-02-30");  
> // days in
> feb must be 01 thru 29
>       TestVF("date","IsValidDate",TRUE,       "1999-02-28");
>       TestVF("date","IsValidDate",FALSE,      "1999-02-29");  
> // 1999 was
> not a leap year
>       TestVF("date","IsValidDate",FALSE,      "1999-02-30");  
> // days in
> feb must be 01 thru 29
> 
>       TestVF("time","IsValidTime",FALSE,      "");            // blank
>       TestVF("time","IsValidTime",TRUE,       "20:05:24");
>       TestVF("time","IsValidTime",FALSE,      "sdghsdh");     
> // contains
> alpha characters
>       TestVF("time","IsValidTime",FALSE,      "4/5/98");      
> // contains
> slashes
>       TestVF("time","IsValidTime",FALSE,      "20:9:7");      // not
> HH:MM:SS format
>       TestVF("time","IsValidTime",TRUE,       "20:09:07");
>       TestVF("time","IsValidTime",FALSE,      "24:09:07");    
> // hour must
> be 00 thru 23
>       TestVF("time","IsValidTime",FALSE,      "20:60:07");    
> // minute
> must be 00 thru 59
>       TestVF("time","IsValidTime",FALSE,      "20:07:60");    
> // second
> must be 00 thru 59
> 
>       TestVF("datetime","IsValidDateTime",FALSE,      "");
> // blank
>       TestVF("datetime","IsValidDateTime",TRUE,       "2000-06-06
> 20:05:24");
>       TestVF("datetime","IsValidDateTime",FALSE,      "2000-06-06
> 20:05:24");   // 2 spaces between date and time
>       TestVF("datetime","IsValidDateTime",FALSE,
> "2000-06-0620:05:24");                // 0 spaces between 
> date and time
>       TestVF("datetime","IsValidDateTime",FALSE,      "2000-06-6
> 20:05:24");           // non-valid date format
>       TestVF("datetime","IsValidDateTime",FALSE,      "2000-06-06
> 20:5:24");            // non-valid time format
>       TestVF("datetime","IsValidDateTime",FALSE,      "2000-06-06");
> // time missing
>       TestVF("datetime","IsValidDateTime",FALSE,      "20:05:24");
> // date missing
>       TestVF("datetime","IsValidDateTime",FALSE,      "20:05:24
> 2000-06-06");         // date and time order reversed
> 
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    "");
> // blank
>       TestVF("timestamp","IsValidTimeStamp",TRUE,     
> "20000606200524");
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    
> "2000066200524");
> // non-valid date format
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    
> "2000060620524");
> // non-valid time format
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    "20000606");
> // time missing
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    "200524");
> // date missing
>       TestVF("timestamp","IsValidTimeStamp",FALSE,    
> "20052420000606");
> // date and time order reversed
> 
>       TestVF("boolean","IsValidBoolean",FALSE,        "");    
>       //
> blank
>       TestVF("boolean","IsValidBoolean",TRUE,         "true");
>       TestVF("boolean","IsValidBoolean",TRUE,         "false");
>       TestVF("boolean","IsValidBoolean",TRUE,         "TRUE");
>       TestVF("boolean","IsValidBoolean",TRUE,         "FALSE");
>       TestVF("boolean","IsValidBoolean",FALSE,        "t");   
>       //
> must be 'true' or 'false'
>       TestVF("boolean","IsValidBoolean",FALSE,        "f");   
>       //
> ditto
>       TestVF("boolean","IsValidBoolean",FALSE,        "0");   
>       //
> ditto
>       TestVF("boolean","IsValidBoolean",FALSE,        "1");   
>       //
> ditto
> 
>       // uncomment the next 2 to test TestVF()
> //    TestVF("boolean","IsValidBoolean",TRUE,         "1");   
>       //
> ditto
> //    TestVF("boolean","IsValidBoolean",FALSE,        
> "true");      //
> ditto
> 
>       echo "done testing.\n";
> }
> 
> TestDatesNumbers();
> 
> ?>
> 
> 
> 
> -----Original Message-----
> From: phpman [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, March 18, 2001 1:44 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] how do i get a variable type? - not that simple
> 
> 
> No guys. that doesn't work. Take this code for example...
> 
> $a="3";
> echo(gettype($a));
> 
> this returns  string
> i need to find it as an integer or real or float for the sql. 
> which by the
> way is MySQL.
> 
> thank you for responding, but I already know that gettype and 
> all the other
> is_*  don't work for this.
> 
> 
> 
> ""phpman"" <[EMAIL PROTECTED]> wrote in message
> 99132e$ol8$[EMAIL PROTECTED]">news:99132e$ol8$[EMAIL PROTECTED]...
> > hello all,
> >
> >  let's say i have this:
> > $a="varone=hello world|vartwo=2.44|varthree=100|";
> >
> > now i do this:
> >
> > $b=explode('|',$a);
> > $z=count($b);
> > for ($x=0;$x<$z;$x++) {
> >     $tmp=explode('=',$b[$x]);
> > ....
> >
> > and i want to find out if  $tmp[1] is a string or an integer (that's
> really
> > all I need to determine so i can put
> > together an SQL statement that puts single quotes around 
> strings and none
> > around integers). Obviously
> > i won't know at design time all of the variables and their 
> values. Thank
> > you.
> >
> > -dave
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> > To contact the list administrators, e-mail: 
> [EMAIL PROTECTED]
> >
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: 
> [EMAIL PROTECTED]
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to