PHP Common detection Function collection

Source: Internet
Author: User



<?php


"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_ ' = array type





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


-------


※checkmoney ($C _money) checks whether the data is in 99999.99 format


※CHECKEMAILADDR ($C _mailaddr) to determine if it is a valid mailing address


※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 string is within the specified length


※checkuser ($C _user) to determine whether a legitimate user name


※checkpassword ($C _passwd) to determine whether a legal user password


※checktelephone ($C _telephone) to determine whether a legal phone number


※checkvaluebetween ($N _var, $N _val1, $N _val2) to determine whether it is a legal value in a range


※checkpost ($C _post) to determine whether the legal ZIP code (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 function in PHP








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)


Role: Determine if a valid mailing address


Parameters: $C _mailaddr (email address to be detected)


Return Value: Boolean value


Note: 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)


Role: To determine whether a valid Web site


Parameters: $C _weburl (URLs to be detected)


Return Value: Boolean value


Note: 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)


Role: Determine if the string is empty


Parameters: $C _char (string to be detected)


Return Value: Boolean value


Note: none





function checkemptystring ($C _char)


{


if (!is_string ($C _char)) return false; Whether it is a string type


if (Empty ($C _char)) return false; Whether it has been set


if ($C _char== ') return false; is empty


return true;


}











Function name: Checklengthbetween ($C _char, $I _len1, $I _len2=100)


Role: Determine whether the string within the specified length


Parameters: $C _char (string to be detected)


$I _len1 (lower bound of the length of the target string)


$I _len2 (upper limit of target string length)


Return Value: Boolean value


Note: none





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)


Role: To determine whether a legitimate user name


Parameters: $C _user (user name to be detected)


Return Value: Boolean value


Note: none





function CheckUser ($C _user)


{


if (! Checklengthbetween ($C _user, 4)) return false; Width test


if (!ereg ("^[_a-za-z0-9]*$", $C _user)) return false; Special character test


return true;


}








Function name: Checkpassword ($C _passwd)


Role: Determine if the password is a legitimate user


Parameters: $C _passwd (password to be detected)


Return Value: Boolean value


Note: 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)


Role: To determine whether a legal telephone number


Parameters: $C _telephone (phone number to be detected)


Return Value: Boolean value


Note: 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)


Role: To determine whether the legal ZIP code (fixed length)


Parameters: $C _post (zip code to check)


Return Value: Boolean value


Note: 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)


Role: Check the size of uploaded pictures


Parameters: $ImageFileName uploaded picture name


Size of $LimitSize requirements


Return Value: Boolean value


Note: none





function Checkimagesize ($ImageFileName, $LimitSize)


{


$size =getimagesize ($ImageFileName);


if ($size [0]> $LimitSize [0]││ $size [1]> $LimitSize [1])


{


Alertexit (' picture size too big ');


return false;


}


return true;


}











Function name: Alert ($C _alert, $I _goback=0)


Role: illegal Operation warning


Parameters: $C _alert (Error message for prompts)


$I _goback (back to that page)


return value: String


Note: 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)


Role: illegal Operation warning


Parameters: $C _alert (Error message for prompts)


$I _goback (back to that page)


return value: String


Note: 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


Parameters: $C _char (strings to be replaced)


return value: String


Note: none





function Replacespecialchar ($C _char)


{


$C _char=htmlspecialchars ($C _char); Converts a special character into HTML format.


$C _char=nl2br ($C _char); Replace the carriage return with the <br>


$C _char=str_replace ("", "", $C _char); Replace a space with


$C _char=str_replace ("A;?", "<", $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.