search
HomeBackend DevelopmentPHP Tutorialphp通用检测函数集合_php技巧

php下一些通用的检测字符串的函数集合,方便大家使用

//【警告】:未经许可请勿随便修改
//-----------------------------------------------------------------------------------
-------
//-----------------------------------------------------------------------------------
-------
//
// 【文件名】: c_check.inc
// 【作 用】: 通用检测函数集
// 【作 者】: 天灰
//
// 【最后修改日期】: 2001/05/11[cxx]
// 【变量定义规则】:‘C_'=字符型,‘I_'=整型,‘N_'=数字型,‘L_'=布尔型,‘A_'=数
组型
//-----------------------------------------------------------------------------------
-------
//-----------------------------------------------------------------------------------
-------
// ※CheckMoney($C_Money) 检查数据是否是
99999.99格式
// ※CheckEmailAddr($C_mailaddr) 判断是否为有效邮件地

// ※CheckWebAddr($C_weburl) 判断是否为有效网址
// ※CheckEmpty($C_char) 判断字符串是否为空
// ※CheckLengthBetween($C_char, $I_len1, $I_len2=100) 判断是否为指定长度内
字符串
// ※CheckUser($C_user) 判断是否为合法用户名
// ※CheckPassword($C_passwd) 判断是否为合法用户密

// ※CheckTelephone($C_telephone) 判断是否为合法电话号

// ※CheckValueBetween($N_var, $N_val1, $N_val2) 判断是否是某一范围内的
合法值
// ※CheckPost($C_post) 判断是否为合法邮编(固
定长度)
// ※CheckExtendName($C_filename,$A_extend) 判断上传文件的扩展名
// ※CheckImageSize($ImageFileName,$LimitSize) 检验上传图片的大小
// ※AlertExit($C_alert,$I_goback=0) 非法操作警告并退出
// ※Alert($C_alert,$I_goback=0) 非法操作警告
// ※ReplaceSpacialChar($C_char) 特殊字符替换函数
// ※ExchangeMoney($N_money) 资金转换函数
// ※WindowLocation($C_url,$C_get="",$C_getOther="") PHP中的window.location
函数
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:CheckMoney($C_Money)
// 作 用:检查数据是否是99999.99格式
// 参 数:$C_Money(待检测的数字)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function CheckMoney($C_Money)
{
if (!ereg("^[0-9][.][0-9]$", $C_Money)) return false;
return true;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:CheckEmailAddr($C_mailaddr)
// 作 用:判断是否为有效邮件地址
// 参 数:$C_mailaddr(待检测的邮件地址)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
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;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:CheckWebAddr($C_weburl)
// 作 用:判断是否为有效网址
// 参 数:$C_weburl(待检测的网址)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function CheckWebAddr($C_weburl)
{
if (!ereg("^http://[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$", $C_weburl))
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:CheckEmpty($C_char)
// 作 用:判断字符串是否为空
// 参 数:$C_char(待检测的字符串)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function CheckEmptyString($C_char)
{
if (!is_string($C_char)) return false; //是否是字符串类型
if (empty($C_char)) return false; //是否已设定
if ($C_char=='') return false; //是否为空
return true;
}
//-----------------------------------------------------------------------------------
-------

//-----------------------------------------------------------------------------------
-------
// 函数名:CheckLengthBetween($C_char, $I_len1, $I_len2=100)
// 作 用:判断是否为指定长度内字符串
// 参 数:$C_char(待检测的字符串)
// $I_len1 (目标字符串长度的下限)
// $I_len2 (目标字符串长度的上限)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
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; 

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

//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckUser($C_user) 
// 作 用:判断是否为合法用户名 
// 参 数:$C_user(待检测的用户名) 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function CheckUser($C_user) 

if (!CheckLengthBetween($C_user, 4, 20)) return false; //宽度检验 
if (!ereg("^[_a-zA-Z0-9]*$", $C_user)) return false; //特殊字符检验 
return true; 

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

//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckPassword($C_passwd) 
// 作 用:判断是否为合法用户密码 
// 参 数:$C_passwd(待检测的密码) 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function CheckPassword($C_passwd) 

if (!CheckLengthBetween($C_passwd, 4, 20)) return false; //宽度检测 
if (!ereg("^[_a-zA-Z0-9]*$", $C_passwd)) return false; //特殊字符检测 
return true; 

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

//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckTelephone($C_telephone) 
// 作 用:判断是否为合法电话号码 
// 参 数:$C_telephone(待检测的电话号码) 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function CheckTelephone($C_telephone) 

if (!ereg("^[+]?[0-9]+([xX-][0-9]+)*$", $C_telephone)) return false; 
return true; 

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


//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckValueBetween($N_var, $N_val1, $N_val2) 
// 作 用:判断是否是某一范围内的合法值 
// 参 数:$N_var 待检测的值 
// $N_var1 待检测值的上限 
// $N_var2 待检测值的下限 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function CheckValueBetween($N_var, $N_val1, $N_val2) 

if ($N_var < $N_var1 ││ $N_var > $N_var2) 

return false; 

return true; 


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


//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckPost($C_post) 
// 作 用:判断是否为合法邮编(固定长度) 
// 参 数:$C_post(待check的邮政编码) 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
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;; 


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


//----------------------------------------------------------------------------------- 
------- 
// 函数名:CheckExtendName($C_filename,$A_extend) 
// 作 用:上传文件的扩展名判断 
// 参 数:$C_filename 上传的文件名 
// $A_extend 要求的扩展名 
// 返回值:布尔值 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function CheckExtendName($C_filename,$A_extend) 

if(strlen(trim($C_filename)) < 5)
{
return 0; //返回0表示没上传图片
}
$lastdot = strrpos($C_filename, "."); //取出.最后出现的位置
$extended = substr($C_filename, $lastdot+1); //取出扩展名

for($i=0;$i{
if (trim(strtolower($extended)) == trim(strtolower($A_extend[$i]))) //转换大
小写并检测
{
$flag=1; //加成功标志
$i=count($A_extend); //检测到了便停止检测
}
}

if($flag<>1) 

for($j=0;$j{
$alarm .= $A_extend[$j]." ";
}
AlertExit('只能上传'.$alarm.'文件!而你上传的是'.$extended.'类型的文件');
return -1; //返回-1表示上传图片的类型不符
}

return 1; //返回1表示图片的类型符合要求
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:CheckImageSize($ImageFileName,$LimitSize)
// 作 用:检验上传图片的大小
// 参 数:$ImageFileName 上传的图片名
// $LimitSize 要求的尺寸
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function CheckImageSize($ImageFileName,$LimitSize)
{
$size=GetImageSize($ImageFileName);
if ($size[0]>$LimitSize[0] ││ $size[1]>$LimitSize[1]) 

AlertExit('图片尺寸过大'); 
return false; 

return true; 

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


//----------------------------------------------------------------------------------- 
------- 
// 函数名:Alert($C_alert,$I_goback=0) 
// 作 用:非法操作警告 
// 参 数:$C_alert(提示的错误信息) 
// $I_goback(返回到那一页) 
// 返回值:字符串 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
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>"; 


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


//----------------------------------------------------------------------------------- 
------- 
//----------------------------------------------------------------------------------- 
------- 
// 函数名:AlertExit($C_alert,$I_goback=0) 
// 作 用:非法操作警告 
// 参 数:$C_alert(提示的错误信息) 
// $I_goback(返回到那一页) 
// 返回值:字符串 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
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; 


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


//----------------------------------------------------------------------------------- 
------- 
// 函数名:ReplaceSpacialChar($C_char) 
// 作 用:特殊字符替换函数 
// 参 数:$C_char(待替换的字符串) 
// 返回值:字符串 
// 备 注:无 
//----------------------------------------------------------------------------------- 
------- 
function ReplaceSpecialChar($C_char) 

$C_char=HTMLSpecialChars($C_char); //将特殊字元转成 HTML 格式。 
$C_char=nl2br($C_char); //将回车替换为
 
$C_char=str_replace(" "," ",$C_char); //替换空格替换为 
$C_char=str_replace("return $C_char;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:ExchangeMoney($N_money)
// 作 用:资金转换函数
// 参 数:$N_money(待转换的金额数字)
// 返回值:字符串
// 备 注:本函数示例:$char=ExchangeMoney(5645132.3155) ==> 
$char='¥5,645,132.31' 
//----------------------------------------------------------------------------------- 
------- 
function ExchangeMoney($N_money) 

$A_tmp=explode(".",$N_money ); //将数字按小数点分成两部分,并存入数组$A_tmp 
$I_len=strlen($A_tmp[0]); //测出小数点前面位数的宽度 

if($I_len%3==0) 

$I_step=$I_len/3; //如前面位数的宽度mod 3 = 0 ,可按,分成$I_step 
部分 
}else 

$step=($len-$len%3)/3+1; //如前面位数的宽度mod 3 != 0 ,可按,分成$I_step 
部分+1 


$C_cur=""; 
//对小数点以前的金额数字进行转换 
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]); 


//对小数点后面的金额的进行转换 
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); 



//加上人民币符号并传出 
$C_cur="¥".$C_cur; 
return $C_cur; 

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


//----------------------------------------------------------------------------------- 
------ 
// 函数名:WindowLocation($C_url,$C_get="",$C_getOther="") 
// 作 用:PHP中的window.location函数 
// 参 数:$C_url 转向窗口的URL 
// $C_get GET方法参数 
// $C_getOther GET方法的其他参数 
// 返回值: 字符串 
// 备 注:无 
//----------------------------------------------------------------------------------- 
----- 
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; 

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

?>  

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor