PHP Common detection Function collection

Source: Internet
Author: User
Tags exit empty ereg strlen trim types of extensions valid zip

?
"Warning": please do not change without permission
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//
"FileName": c_check.inc
"Role": a set of common detection functions
"Author": Sky Ash
//
"Last Modified Date": 2001/05/11[cxx]
"Variable definition rule": ' C_ ' = character type, ' i_ ' = integral type, ' n_ ' = numeric, ' L_ ' = boolean, ' a_ ' = number
Group type
//----------------------------------------------------------------------
//----------------------------------------------------------------------
※checkmoney ($C _money) checks whether the data is
99999.99 format
※CHECKEMAILADDR ($C _mailaddr) to determine if it is a valid message
Access
※CHECKWEBADDR ($C _weburl) to determine whether it is a valid URL
※checkempty ($C _char) to determine whether the string is empty
※checklengthbetween ($C _char, $I _len1, $I _len2=100) determines whether the specified length of
String
※checkuser ($C _user) to determine whether a legitimate user name
※checkpassword ($C _passwd) to determine whether a legal user is secret
Code
※checktelephone ($C _telephone) to determine whether a legal telephone number
Code
※checkvaluebetween ($N _var, $N _val1, $N _val2) to determine whether a range of
Legal value
※checkpost ($C _post) to determine whether the legal ZIP code (solid
Fixed length)
※checkextendname ($C _filename, $A _extend) to determine the extension of the uploaded file
※checkimagesize ($ImageFileName, $LimitSize) Check the size of uploaded pictures
※alertexit ($C _alert, $I _goback=0) Illegal operation warning and exiting
※alert ($C _alert, $I _goback=0) Illegal operation warning
※replacespacialchar ($C _char) special character substitution function
※exchangemoney ($N _money) Capital conversion function
※windowlocation ($C _url, $C _get= "", $C _getother= "") Window.location in PHP
Function
//----------------------------------------------------------------------
//----------------------------------------------------------------------
Function name: Checkmoney ($C _money)
Function: Check whether the data is 99999.99 format
Parameters: $C _money (number to be detected)
Return Value: Boolean value
Note: none
//----------------------------------------------------------------------
function Checkmoney ($C _money)
{
if (!ereg ("^[0-9][.] [0-9]$ ", $C _money)) return false;
return true;
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
//Function Name: Checkemailaddr ($C _ MAILADDR)
//function: To determine if it is a valid mailing address
//parameter: $C _mailaddr (email address to be detected)
//Return value: Boolean
//Memo: None
//---------------------- ------------------------------------------------
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)
//function: To determine if it is a valid URL
//parameter: $C _weburl (address to be detected)
//Return value: Boolean
//Memo: None
//------------------------------ ----------------------------------------
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)
//function: To determine whether the string is empty
//parameter: $C _char (the string to be detected)
//Return value: Boolean
//Memo: None
//-------------------------------------- --------------------------------
Function checkemptystring ($C _char)
{
if (!is_string ($C _char)) return False Whether it is a string type
if (empty ($C _char)) return false;/is set
if ($C _char== ") return false;//null
return true;
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//Function Name: Checklengthbetween ($C _ Char, $I _len1, $I _len2=100)
//function: To determine whether the string within the specified length
//parameter: $C _char (the string to be detected)
//$I _len1 (lower bound of the target string length)
//$I _len 2 (upper limit of target string length)
//Return value: Boolean
//Memo: 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 whether the legal user name
//Parameters: $C _user (user name to be detected)
//Return value: Boolean
//Memo: None
//-------------------------------------------- --------------------------
Function CheckUser ($C _user)
{
if (!) Checklengthbetween ($C _user, 4)) return false; Width Check
if (!ereg ("^[_a-za-z0-9]*$", $C _user)) return false;//special character validation
return true;
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//Function Name: Checkpassword ($C _passwd)
//function: To determine whether the password is legitimate user
//parameter: $C _passwd (password to be detected)
//Return value: Boolean
//Memo: None
//----------------------------------- -----------------------------------
Function Checkpassword ($C _passwd)
{
if (!) Checklengthbetween ($C _passwd, 4)) return false; Width detection
if (!ereg ("^[_a-za-z0-9]*$", $C _passwd)) return false;//special character detection
return true;
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
//Function Name: Checktelephone ($C _ Telephone)
//function: To determine whether the legal phone number
//parameter: $C _telephone (pending phone number)
//Return value: Boolean
//Memo: None
//-------------------- --------------------------------------------------
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)
Role: To determine whether a range of legal values
Parameters: $N _var value to be detected
$N the upper limit of the _VAR1 value to be detected
$N _var2 the lower bound of the value to be detected
Return Value: Boolean value
Note: none
//----------------------------------------------------------------------
function Checkvaluebetween ($N _var, $N _val1, $N _val2)
{
if ($N _var < $N _var1││ $N _var > $N _var2)
{
return false;
}
return true;

}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
//Function Name: Checkpost ($C _post)
//function: To determine whether it is legal zip (fixed length)
//parameter: $C _post (zip code to check)
//Return value: Boolean
//Memo: None
//---------------------------- ------------------------------------------
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)
Role: Upload file extension to determine the name
Parameters: $C _filename uploaded file name
$A extension required by _extend
Return Value: Boolean value
Note: none
//----------------------------------------------------------------------
function Checkextendname ($C _filename, $A _extend)
{
if (strlen ($C _filename)) < 5)
{
return 0; return 0 means no pictures uploaded
}
$lastdot = Strrpos ($C _filename, "."); Take out. The last place to appear
$extended = substr ($C _filename, $lastdot + 1); Remove extension

For ($i =0 $i <count ($A _extend), $i + +)//For testing
{
if (Trim (Strtolower ($extended)) = = Trim (Strtolower ($A _extend[$i)))//Convert Large
Lowercase and detects
{
$flag = 1; Plus success Sign
$i =count ($A _extend); Stop detection when detected.
}
}

if ($flag <>1)
{
For ($j =0 $j <count ($A _extend); $j + +)//list the types of extensions allowed to upload
{
$alarm. = $A _extend[$j]. " ";
}
Alertexit (' Upload only '. $alarm. ' File! And you're uploading a '. $extended. ' Type of file ');
return-1; Return-1 indicates the type of upload image does not match
}

return 1; Returns 1 indicating that the type of the picture meets the requirements
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
//Function Name: Checkimagesize ($ Imagefilename, $LimitSize)
//function: Check upload picture size
//parameter: $ImageFileName upload picture name
//$LimitSize Required size
//Return value: Boolean
//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)
//function: Illegal operation warning
//parameter: $C _alert (hint error message)
//$I _goback (back to that page)
//return value: String
//Memo: None
//------ ----------------------------------------------------------------
Function Alert ($C _alert, $I _goback=0)
{
if ($I _goback<>0)
{
echo <script>alert (' $C _alert '); History.go ($I _goback);</script> " ;
}
Else
{
echo "<script>alert (' $C _alert ');</script>";
}

//----------------------------------------------------------------------


//----------------------------------------------------------------------
//-------------------------- --------------------------------------------
//Function Name: Alertexit ($C _alert, $I _goback=0)
//function: Illegal operation warning
//parameter: $ C_alert (hint error message)
//$I _goback (back to that page)
//return value: String
//Memo: None
//------------------------------------------ ----------------------------
Function Alertexit ($C _alert, $I _goback=0)
{
if ($I _goback<>0)
{
echo "<script>alert (' $C _alert '); History.go ($I _goback);</script>";
Exit;
}
Else
{
echo "<script>alert (' $C _alert ');</script>";
Exit;
}

//----------------------------------------------------------------------


//----------------------------------------------------------------------
//Function Name: Replacespacialchar ($C _char)
//function: Special character substitution function
//parameter: $C _char (the string to be replaced)
//return value: String
//Memo: None
//--------------------------------- -------------------------------------
Function Replacespecialchar ($C _char)
{
$C _char=htmlspecialchars ( $C _char); Converts a special character into HTML format.
$C _char=nl2br ($C _char);//replace carriage return with <br>
$C _char=str_replace ("", "", $C _char); Replace spaces with
$C _char=str_replace ("" "," < ", $C _char);//Replace PHP tags
return $C _char;
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
Function name: Exchangemoney ($N _money)
Function: Capital Conversion functions
Parameters: $N _money (number of amounts to convert)
return value: String
Note: This function example: $char =exchangemoney (5645132.3155) ==>
$char = ' ¥5,645,132.31 '
//----------------------------------------------------------------------
function Exchangemoney ($N _money)
{
$A _tmp=explode (".", $N _money); Divide the number into two parts by a decimal point and save it in an array $a_tmp
$I _len=strlen ($A _tmp[0]); Measure the width of the number of digits before the decimal point

if ($I _len%3==0)
{
$I _step= $I _LEN/3; such as the width of the front digits mod 3 = 0, can be pressed, divided into $i_step
Part
}else
{
$step = ($len-$len%3)/3+1; such as the width of the front 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]);
}

Convert the amount after 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);
}
}

Plus RMB symbol and outgoing
$C _cur= "¥". $C _cur;
return $C _cur;
}
//----------------------------------------------------------------------


//---------------------------------------------------------------------
Function name: windowlocation ($C _url, $C _get= "", $C _getother= "")
Function: window.location function in PHP
Parameters: $C _url to the URL of the window
$C _get Get method parameters
$C other parameters for the _getother get method
return value: String
Note: none
//--------------------------------------------------------------------
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;
}
//-----------------------------------------------------------------------------------

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.