2010/11/01

PHP Check and Validate

<?php

class check{

var $bench_start = 0;

function check(){
}

function check_int($value){
$help = (int)$value;
if(strcmp($help, $value)){
return false;
}
else{
return true;
}
}

function check_empty($value, $type = 1){
if($type == 0){
if(empty($value)){
return true;
}
else{
return false;
}
}
else{
if(!empty($value) || $value == "0"){
return true;
}
else{
return false;
}
}
}

function get_max($value_arr){
$max = $value_arr[0];
$i = 0;
$arr_index = $i;
foreach($value_arr as $val){
if($val > $max){
$max = $val;
$arr_index = $i;
}
$i++;
}
$return["arr_index"] = $arr_index;
$return["max_val"] = $max;
return $return;
}

function get_min($value_arr){
$min = $value_arr[0];
$i = 0;
$arr_index = $i;
foreach($value_arr as $val){
if($val < $min){
$min = $val;
$arr_index = $i;
}
$i++;
}
$return["arr_index"] = $arr_index;
$return["min_val"] = $min;
return $return;
}

function max($value, $max){
if($value > $max){
return false;
}
else{
return true;
}
}

function min($value, $min){
if($value < $min){
return false;
}
else{
return true;
}
}

function max_strlen($string, $max){
if(strlen($string) > $max){
return false;
}
else{
return true;
}
}

function min_strlen($string, $min){
if(strlen($string) < $min){
return false;
}
else{
return true;
}
}

function check_email($value){
$reg_exp="^($-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~�])+@($-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~�]+\\.)+$a-zA-Z]{2,6}\$";
$is_valid = eregi($reg_exp, $value);
if(!$is_valid){
return false;
}
else{
return true;
}
}

function check_plz($value, $check_strlen = 1){
$help = $value;
$first_letter = substr($value, 0, 1);
$is_null = false;
if(!strcmp($first_letter, "0")){
$is_null = true;
}
$plz= (int)$value;
if($is_null){
$plz = "0".$plz;
}
switch($check_strlen){
case 1:
if(strcmp($plz, $help) || strlen($plz) <5){
return false;
}
else{
return true;
}
break;
case 0:
if(strcmp($plz, $help)){
return false;
}
else{
return true;
}
break;
default:
echo "False type for check_strlen";
}
}

function sum($val_array = array(), $sum_val){
$sum = 0;
foreach($val_array as $val){
$sum += $val;
}
if($sum == $sum_val){
return true;
}
else{
return false;
}
}

function check_date($date){
$d = explode(".", $date);
if(strlen(@$d$2]) > 2){
@$d$2] = substr($d$2], strlen($d$2]) - 2 , 2);
}
if(checkdate(@$d$1], @$d$0], @$d$2])){
return true;
}
else{
return false;
}
}

function german_chars($string){
$string = str_replace("A", "&Auml;", $string);
$string = str_replace("O", "&Ouml;", $string);
$string = str_replace("U", "&Uuml;", $string);
$string = str_replace("a", "&auml;", $string);
$string = str_replace("o", "&ouml;", $string);
$string = str_replace("u", "&uuml;", $string);
$string = str_replace("s", "&szlig;", $string);
$string = str_replace("e", "&eacute;", $string);
return $string;
}

function is_hostname($url){
// (type of stream )(domain ).(tld )
$reg_exp = "/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/)*($a-z]{1,}$\w-.]{0,}).($a-z]{2,6})$/i";
if(!preg_match($reg_exp, $url, $result)){
return false;
}
return true;
}

function is_url($url){
// (type of stream )(domain ).(tld )(scriptname or dirs )
$reg_exp = "/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/)($a-z]{1,}$\w-.]{0,}).($a-z]{2,6})(\/{1}$\w_]{1}$\/\w-&?=_%]{0,}(.{1}$\/\w-&?=_%]{0,})*)*$/i";
if(!preg_match($reg_exp, $url, $result)){
return false;
}
return true;
}

function base_convert($num, $from){
$result["binary"] = base_convert($num, $from, 2);
$result["octal"] = base_convert($num, $from, 8);
$result["decimal"] = base_convert($num, $from, 10);
$result["hex"] = base_convert($num, $from, 16);
return $result;
}

function random_nr($min = 0, $max = 100000000000000){
return (mt_rand($min, $max));
}

function random_text($prefix = "generate_random_id"){
return md5(uniqid($prefix));
}

function microtime(){
$time = microtime();
$time = explode(" ", $time);
return $time[1] + $time[0];
}

function start_benchmark(){
$GLOBALS["bench_start_time"] = check::microtime();
}

function stop_benchmark(){
$GLOBALS["bench_end_time"] = check::microtime();
$time = $GLOBALS["bench_end_time"] - $GLOBALS["bench_start_time"];
$time = explode(".", $time);
$time[1] = substr($time[1], 0, 4);
$days = 0;
$hours = 0;
$mins = 0;
$seconds = $time[0];
while($seconds > 60){
$mins++;
$seconds -= 60;
}
while($mins > 60){
$hours++;
$mins -= 60;
}
while($hours > 24){
$days++;
$hours -= 24;
}
$durability = $days." 耗時 ".$hours." 時 ".$mins." 分 ".$seconds.".".$time[1]." 秒";
echo "<br><b>腳本時間: ".$durability."</b>";
}

function debug($array, $blank=0, $echo_class=0){
$multi = false;
if(is_object($array) && !$echo_class){
echo "<b>調試的對象: ".get_class($array)."</b><br>";
$multi = true;
}
if(is_array($array) && !$echo_class){
echo "<b>調試一個陣列</b><br>";
$multi = true;
}
if(!$multi && !$echo_class){
echo "<b>單變數調試</b><br>";
echo $array;
}
else{
$blanks = "";
for($i=0; $i<$blank; $i++){
$blanks .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
while(list($key, $val) = each ($array)){
if(is_array($val) || is_object($val)){
if(is_object($val)){
echo $blanks.$key." => "."<b>".$val." ".get_class($val)."</b><br>";
}
else{
echo $blanks.$key." => "."<b>".$val."</b><br>";
}
debug($val, $blank+1, 1);
}
else{
echo $blanks.$key." => ".$val."<br>";
}
}
}
}

function object_info($object){
$methods = get_class_methods(get_class($object));
$vars = get_class_vars(get_class($object));
echo "<b>概述對象類:".get_class($object)."</b><br>";
echo "<b>父類:".get_parent_class($object)."</b><br>";
echo "<b>概述方法類:<br></b>";
foreach($methods as $method){
echo "&nbsp;&nbsp;&nbsp;&nbsp;".$method."<br>";
}
echo "<b>概述變量類: (名稱 => 預設值)<br></b>";
foreach($vars as $var => $val){
echo "&nbsp;&nbsp;&nbsp;&nbsp;".$var." => ".$val."<br>";
}
}
}
?>



<?php
/**
* Validation class for validation of misc strings
*
* @author Sven Wagener <wagener_at_indot_de>
* @include Funktion:_include_
*/
class validate{
var $country_code;
var $string;

/**
* Regular expressions for postcode of the following countries
* at = austria
* au = australia
* ca = canada
* de = german
* ee = estonia
* nl = netherlands
* it = italy
* pt = portugal
* se = sweden
* uk = united kingdom
* us = united states
**/
var $pattern_postcode=array(
'at'=>'^$0-9]{4,4},
'au'=>'^$2-9]$0-9]{2,3},
'ca'=>'^$a-zA-Z].$0-9].$a-zA-Z].\s$0-9].$a-zA-Z].$0-9].',
'de'=>'^$0-9]{5,5},
'ee'=>'^$0-9]{5,5},
'nl'=>'^$0-9]{4,4}\s$a-zA-Z]{2,2},
'it'=>'^$0-9]{5,5},
'pt'=>'^$0-9]{4,4}-$0-9]{3,3},
'se'=>'^$0-9]{3,3}\s$0-9]{2,2},
'uk'=>'^($A-Z]{1,2}$0-9]{1}$0-9A-Z]{0,1}) ?($0-9]{1}$A-Z]{1,2}),
'us'=>'^$0-9]{5,5}$\-]{0,1}$0-9]{4,4}'

);

var $pattern_email='^($a-zA-Z0-9_\-\.]+)@((\$0-9]{1,3}\.$0-9]{1,3}\.$0-9]{1,3}\.)|(($a-zA-Z0-9\-]+\.)+))($a-zA-Z]{2,4}|$0-9]{1,3})(\]?)$ ';
var $pattern_ip='($0-9]{1,3}\.$0-9]{1,3}\.$0-9]{1,3}\.$0-9]{1,3})';
var $pattern_url='^(http|ftp)://(www\.)?.+\.(com|net|org);

/**
* The constructor of the validation class
* @param string $string the string which have to be validated
* @desc The constructor of the validation class
*/
function validate($string){
$this->string=$string;
}

/**
* Validates the string if it consists of alphabetical chars
* @param int $num_chars the number of chars in the string
* @param string $behave defines how to check the string: min, max or exactly number of chars
* @desc Validates the string if it consists of alphabetical chars
*/
function alpha($num_chars,$behave){
if($behave=="min"){
$pattern="^$a-zA-Z]{".$num_chars.",}$";
}else if ($behave=="max"){
$pattern="^$a-zA-Z]{0,".$num_chars."}$";
}else if ($behave=="exactly"){
$pattern="^$a-zA-Z]{".$num_chars.",".$num_chars."}$";
}
return ereg($pattern,$this->string);
}

/**
* Validates the string if it consists of lowercase alphabetical chars
* @param int $num_chars the number of chars in the string
* @param string $behave defines how to check the string: min, max or exactly number of chars
* @desc Validates the string if it consists of lowercase alphabetical chars
*/
function alpha_lowercase($num_chars,$behave){
if($behave=="min"){
$pattern="^$a-z]{".$num_chars.",}$";
}else if ($behave=="max"){
$pattern="^$a-z]{0,".$num_chars."}$";
}else if ($behave=="exactly"){
$pattern="^$a-z]{".$num_chars.",".$num_chars."}$";
}
return ereg($pattern,$this->string);

}

/**
* Validates the string if it consists of uppercase alphabetical chars
* @param int $num_chars the number of chars in the string
* @param string $behave defines how to check the string: min, max or exactly number of chars
* @desc Validates the string if it consists of uppercalse alphabetical chars
*/
function alpha_uppercase($num_chars,$behave){
if($behave=="min"){
$pattern="^$A-Z]{".$num_chars.",}$";
}else if ($behave=="max"){
$pattern="^$A-Z]{0,".$num_chars."}$";
}else if ($behave=="exactly"){
$pattern="^$A-Z]{".$num_chars.",".$num_chars."}$";
}
return ereg($pattern,$this->string);

}

/**
* Validates the string if it consists of numeric chars
* @param int $num_chars the number of chars in the string
* @param string $behave defines how to check the string: min, max or exactly number of chars
* @desc Validates the string if it consists of numeric chars
*/
function numeric($num_chars,$behave){
if($behave=="min"){
$pattern="^$0-9]{".$num_chars.",}$";
}else if ($behave=="max"){
$pattern="^$0-9]{0,".$num_chars."}$";
}else if ($behave=="exactly"){
$pattern="^$0-9]{".$num_chars.",".$num_chars."}$";
}
return ereg($pattern,$this->string);
}

/**
* Validates the string if it consists of alphanumerical chars
* @param int $num_chars the number of chars in the string
* @param string $behave defines how to check the string: min, max or exactly number of chars
* @desc Validates the string if it consists of alphanumerical chars
*/
function alpha_numeric($num_chars,$behave){
if($behave=="min"){
$pattern="^$0-9a-zA-Z]{".$num_chars.",}$";
}else if ($behave=="max"){
$pattern="^$0-9a-zA-Z]{0,".$num_chars."}$";
}else if ($behave=="exactly"){
$pattern="^$0-9a-zA-Z]{".$num_chars.",".$num_chars."}$";
}
return ereg($pattern,$this->string);
}

/**
* Validates the string if its a valid postcode
* @param string $country_code the country code for the country of the postcode (de,en)
* @desc Validates the string if its a valid postcode
*/
function postcode($country_code){
if(array_key_exists($country_code,$this->pattern_postcode)){
return ereg($this->pattern_postcode$country_code],$this->string);
}else{
return false;
}
}

/**
* Validates the string if its a valid email address
* @desc Validates the string if its a valid email adress
*/
function email(){
return ereg($this->pattern_email,$this->string);
}

/**
* Validates the string if its a valid ip address
* @desc Validates the string if its a valid ip address
*/
function ip_address(){
return ereg($this->pattern_ip,$ip);
}

/**
* Validates the string if its a valid URL
* @desc Validates the string if its a valid URL
*/
function url(){
return ereg($this->pattern_url,$ip);
}
}
?>

沒有留言: