PHP universal collection of detection functions

  • 2020-03-31 21:31:08
  • OfStack

// does CheckMoney($C_Money) checks whether the data is in the 99999.99 format
// does CheckEmailAddr($C_mailaddr) determines whether it is a valid email address
// does CheckWebAddr($C_weburl) determines if it is a valid url
// does CheckEmpty($C_char) checks if the string is empty
// does CheckLengthBetween($C_char, $I_len1, $I_len2=100) to see if it is a string of the specified length
// does CheckUser($C_user) determines whether it is a valid user name
// does CheckPassword($C_passwd) determines whether it is a valid user password
// does CheckTelephone($C_telephone) determines whether it is a valid telephone number
// does CheckValueBetween($N_var, $N_val1, $N_val2
// does CheckPost($C_post) determines whether the zip code is valid or not (fixed length)
// does CheckExtendName($C_filename,$A_extend) determines the extension of the uploaded file
// does CheckImageSize($ImageFileName,$LimitSize) checks the size of the uploaded image
// does AlertExit($C_alert,$I_goback=0) illegal operation warning and exit
// does Alert($C_alert,$I_goback=0) illegal operation warning
// does ReplaceSpacialChar($C_char) special character replacement function
// does ExchangeMoney($N_money) fund conversion function
// does WindowLocation($C_url,$C_get= "",$C_getOther=" ") window.location function in PHP

<?php 
//Function name: CheckMoney($C_Money)
//For use: check whether the data is in 99999.99 format
//Parameters: $C_Money (number to be detected)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckMoney($C_Money) 
{ 
if (!ereg("^[0-9][.][0-9]$", $C_Money)) return false; 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckEmailAddr($C_mailaddr)
//Use: to determine if it is a valid email address
//Parameters: $C_mailaddr (mail address to be detected)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckEmailAddr($C_mailaddr) 
{ 
if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$", 
$C_mailaddr)) 
//(!ereg("^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$", 
$c_mailaddr)) 
{ 
return false; 
} 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckWebAddr($C_weburl)
//Use: to determine if it is a valid url
//Parameters: $C_weburl (url to be detected)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckWebAddr($C_weburl) 
{ 
if (!ereg("^http://[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$", $C_weburl)) 
{ 
return false; 
} 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckEmpty($C_char)
//Use: to determine whether a string is empty
//Arguments: $C_char (string to be detected)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckEmptyString($C_char) 
{ 
if (!is_string($C_char)) return false; //Is it a string type
if (empty($C_char)) return false; //Is it set
if ($C_char=='') return false; //Whether is empty
return true; 
} 
//----------------------------------------------------------------------------------- 
//CheckLengthBetween($C_char, $I_len1, $I_len2=100)
//Use: determines whether a string is within the specified length
//Arguments: $C_char (string to be detected)
//$I_len1 (lower limit of target string length)
//$I_len2 (upper limit of target string length)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckLengthBetween($C_cahr, $I_len1, $I_len2=100) 
{ 
$C_cahr = trim($C_cahr); 
if (strlen($C_cahr) < $I_len1) return false; 
if (strlen($C_cahr) > $I_len2) return false; 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckUser($C_user)
//Use: to determine if it is a valid user name
//Parameters: $C_user (user name to be detected)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckUser($C_user) 
{ 
if (!CheckLengthBetween($C_user, 4, 20)) return false; //The width of the test
if (!ereg("^[_a-zA-Z0-9]*$", $C_user)) return false; //Special character check
return true; 
} 
< ?php 
//Function name: CheckPassword($C_passwd)
//Use: to determine whether it is a valid user password
//Parameters: $C_passwd
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckPassword($C_passwd) 
{ 
if (!CheckLengthBetween($C_passwd, 4, 20)) return false; //The width of the test
if (!ereg("^[_a-zA-Z0-9]*$", $C_passwd)) return false; //Special character detection
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckTelephone($C_telephone)
//Use: to determine if it is a valid phone number
//Parameters: $C_telephone (telephone number to be tested)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckTelephone($C_telephone) 
{ 
if (!ereg("^[+]?[0-9]+([xX-][0-9]+)*$", $C_telephone)) return false; 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckValueBetween($N_var, $N_val1, $N_val2)
//Use: to determine whether a valid value is in a range
//Parameter: the value of $N_var to be detected
//$N_var1 upper limit of the value to be detected
//$N_var2 the lower limit of the value to be detected
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckValueBetween($N_var, $N_val1, $N_val2) 
{ 
if ($N_var < $N_var1  │ │  $N_var > $N_var2) 
{ 
return false; 
} 
return true; 
} 
?> 
< ?php 
//Function name: CheckPost($C_post)
//Use: to determine whether the zip code is valid (fixed length)
//Parameters: $C_post (zip code to be checked)
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckPost($C_post) 
{ 
$C_post=trim($C_post); 
if (strlen($C_post) == 6) 
{ 
if(!ereg("^[+]?[_0-9]*$",$C_post)) 
{ 
return true;; 
}else 
{ 
return false; 
} 
}else 
{ 
return false;; 
} 
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckExtendName($C_filename,$A_extend)
//For use: upload file extension judgment
//Arguments: $C_filename the name of the file to be uploaded
//The required extension for $A_extend
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckExtendName($C_filename,$A_extend) 
{ 
if(strlen(trim($C_filename)) < 5) 
{ 
return 0; //Returning 0 means no picture has been uploaded
} 
$lastdot = strrpos($C_filename, "."); //Take out the last position
$extended = substr($C_filename, $lastdot+1); //Fetch extension
for($i=0;$i{ 
if (trim(strtolower($extended)) == trim(strtolower($A_extend[$i]))) //Transform large
 Lowercase and detection  
{ 
$flag=1; //Add success mark
$i=count($A_extend); //Detect and stop detection
} 
} 
if($flag<>1) 
{ 
for($j=0;$j{ 
$alarm .= $A_extend[$j]." "; 
} 
AlertExit(' Can only upload '.$alarm.' File! And what you're uploading is '.$extended.' Type of file '); 
return -1; //Returning -1 indicates that the uploaded image is of a different type
} 
return 1; //Returning 1 indicates that the type of image meets the requirements
} 
//----------------------------------------------------------------------------------- 
//Function name: CheckImageSize($ImageFileName,$LimitSize)
//For use: check the size of the uploaded image
//Parameters: $ImageFileName upload image name
//$LimitSize requires size
//Return value: Boolean
//Case note: no
//----------------------------------------------------------------------------------- 
function CheckImageSize($ImageFileName,$LimitSize) 
{ 
$size=GetImageSize($ImageFileName); 
if ($size[0]>$LimitSize[0]  │ │  $size[1]>$LimitSize[1]) 
{ 
AlertExit(' Picture size is too large '); 
return false; 
} 
return true; 
} 
//----------------------------------------------------------------------------------- 
//Function name: Alert($C_alert,$I_goback=0)
//Use: illegal operation warning
//Parameters: $C_alert
//$I_goback (back to that page)
//Return value: string
//Case note: no
//----------------------------------------------------------------------------------- 
function Alert($C_alert,$I_goback=0) 
{ 
if($I_goback<>0) 
{ 
echo " "; 
} 
else 
{ 
echo " "; 
} 
} 
?> 
< ?php 
//Function name: AlertExit($C_alert,$I_goback=0)
//Use: illegal operation warning
//Parameters: $C_alert
//$I_goback (back to that page)
//Return value: string
//Case note: no
//----------------------------------------------------------------------------------- 
function AlertExit($C_alert,$I_goback=0) 
{ 
if($I_goback<>0) 
{ 
echo " "; 
exit; 
} 
else 
{ 
echo " "; 
exit; 
} 
} 
//----------------------------------------------------------------------------------- 
//Function name: ReplaceSpacialChar($C_char)
//Use: special character substitution function
//Arguments: $C_char (string to be replaced)
//Return value: string
//Caveat: there is a problem with this function and it needs to be tested before it can be used
//----------------------------------------------------------------------------------- 
function ReplaceSpecialChar($C_char) 
{ 
$C_char=HTMLSpecialChars($C_char); //Converts special characters to HTML format.
$C_char=nl2br($C_char); //Replace the return with
$C_char=str_replace(" "," ",$C_char); //Replace the space with
return $C_char; 
} 
//----------------------------------------------------------------------------------- 
//Function name: ExchangeMoney($N_money)
//For use: fund conversion function
//Parameters: $N_money (amount to be converted)
//Return value: string
//$char=ExchangeMoney(5645132.3155) ==> $char = '$5645132.31...
//----------------------------------------------------------------------------------- 
function ExchangeMoney($N_money) 
{ 
$A_tmp=explode(".",$N_money ); //Divide the Numbers into two decimal places and store them in an array of $A_tmp
$I_len=strlen($A_tmp[0]); //Measure the width of the number of digits in front of the decimal point
if($I_len%3==0) 
{ 
$I_step=$I_len/3; //If the width of the preceding digits mod 3 = 0, press, split into $I_step
}else 
{ 
$step=($len-$len%3)/3+1; //As the width of the preceding digits mod 3! = 0, can be pressed, divided into $I_step part +1
} 
$C_cur=""; 
//Convert the amount number before the decimal point
while($I_len<>0) 
{ 
$I_step--; 
if($I_step==0) 
{ 
$C_cur .= substr($A_tmp[0],0,$I_len-($I_step)*3); 
}else 
{ 
$C_cur .= substr($A_tmp[0],0,$I_len-($I_step)*3).","; 
} 
$A_tmp[0]=substr($A_tmp[0],$I_len-($I_step)*3); 
$I_len=strlen($A_tmp[0]); 
} 
//To convert the amount behind the decimal point
if($A_tmp[1]=="") 
{ 
$C_cur .= ".00"; 
}else 
{ 
$I_len=strlen($A_tmp[1]); 
if($I_len<2) 
{ 
$C_cur .= ".".$A_tmp[1]."0"; 
}else 
{ 
$C_cur .= ".".substr($A_tmp[1],0,2); 
} 
} 
//Add the RMB symbol and spread it out
$C_cur=" RMB ".$C_cur; 
return $C_cur; 
} 
//----------------------------------------------------------------------------------- 
//The function name: WindowLocation ($C_url, $C_get = "", $C_getOther =" ")
//Use: the window.location function in PHP
//Parameters: $C_url to the URL of the window
//$C_get GET method parameter
//Other parameters of the $C_getOther GET method
//Return value: string
//Case note: no
//----------------------------------------------------------------------------------- 
function WindowLocation($C_url,$C_get="",$C_getOther="") 
{ 
if($C_get == "" && $C_getOther == "") 
if($C_get == "" && $C_getOther <> ""){$C_target=""window.location='$C_url? 
$C_getOther='+this.value"";} 
if($C_get <> "" && $C_getOther == ""){$C_target=""window.location='$C_url? 
$C_get'"";} 
if($C_get <> "" && $C_getOther <> ""){$C_target=""window.location='$C_url? 
$C_get&$C_getOther='+this.value"";} 
return $C_target; 
} 
?> 

Related articles: