选择显示字体大小

php通用检测函数集(转)


   php通用检测函数集 转http://wb.josetech.com/userhome/4/jaxp/add-ons/news/13/20020828/51.html
发布时间:2002-08-28 17:52:33 新闻来源:  
  
<?
//【警告】:未经许可请勿随便修改
//-----------------------------------------------------------------------------------
-------
//-----------------------------------------------------------------------------------
-------
//
// 【文件名】: c_check.inc
// 【作 用】: 通用检测函数集
// 【作 者】: 天灰
//
// 【最后修改日期】: 2001/05/11[cxx]
// 【变量定义规则】:‘c_’=字符型,‘i_’=整型,‘n_’=数字型,‘l_’=布尔型,‘a_’=数
组型
//-----------------------------------------------------------------------------------
-------
//-----------------------------------------------------------------------------------
-------
// ※checkmoney(&#36;c_money) 检查数据是否是
99999.99格式
// ※checkemailaddr(&#36;c_mailaddr) 判断是否为有效邮件地

// ※checkwebaddr(&#36;c_weburl) 判断是否为有效网址
// ※checkempty(&#36;c_char) 判断字符串是否为空
// ※checklengthbetween(&#36;c_char, &#36;i_len1, &#36;i_len2=100) 判断是否为指定长度内
字符串
// ※checkuser(&#36;c_user) 判断是否为合法用户名
// ※checkpassword(&#36;c_passwd) 判断是否为合法用户密

// ※checktelephone(&#36;c_telephone) 判断是否为合法电话号

// ※checkvaluebetween(&#36;n_var, &#36;n_val1, &#36;n_val2) 判断是否是某一范围内的
合法值
// ※checkpost(&#36;c_post) 判断是否为合法邮编(固
定长度)
// ※checkextendname(&#36;c_filename,&#36;a_extend) 判断上传文件的扩展名
// ※checkimagesize(&#36;imagefilename,&#36;limitsize) 检验上传图片的大小
// ※alertexit(&#36;c_alert,&#36;i_goback=0) 非法操作警告并退出
// ※alert(&#36;c_alert,&#36;i_goback=0) 非法操作警告
// ※replacespacialchar(&#36;c_char) 特殊字符替换函数
// ※exchangemoney(&#36;n_money) 资金转换函数
// ※windowlocation(&#36;c_url,&#36;c_get=&quot;&quot;,&#36;c_getother=&quot;&quot;) php中的window.location
函数
//-----------------------------------------------------------------------------------
-------


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


//-----------------------------------------------------------------------------------
-------
// 函数名:checkemailaddr(&#36;c_mailaddr)
// 作 用:判断是否为有效邮件地址
// 参 数:&#36;c_mailaddr(待检测的邮件地址)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkemailaddr(&#36;c_mailaddr)
{
if (!eregi(&quot;^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*&#36;&quot;,
&#36;c_mailaddr))
//(!ereg(&quot;^[_a-za-z0-9-]+(.[_a-za-z0-9-]+)*@[_a-za-z0-9-]+(.[_a-za-z0-9-]+)*&#36;&quot;,
&#36;c_mailaddr))
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:checkwebaddr(&#36;c_weburl)
// 作 用:判断是否为有效网址
// 参 数:&#36;c_weburl(待检测的网址)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkwebaddr(&#36;c_weburl)
{
if (!ereg(&quot;^http://[_a-za-z0-9-]+(.[_a-za-z0-9-]+)*&#36;&quot;, &#36;c_weburl))
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------------
-------


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

//-----------------------------------------------------------------------------------
-------
// 函数名:checklengthbetween(&#36;c_char, &#36;i_len1, &#36;i_len2=100)
// 作 用:判断是否为指定长度内字符串
// 参 数:&#36;c_char(待检测的字符串)
// &#36;i_len1 (目标字符串长度的下限)
// &#36;i_len2 (目标字符串长度的上限)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checklengthbetween(&#36;c_cahr, &#36;i_len1, &#36;i_len2=100)
{
&#36;c_cahr = trim(&#36;c_cahr);
if (strlen(&#36;c_cahr) < &#36;i_len1) return false;
if (strlen(&#36;c_cahr) > &#36;i_len2) return false;
return true;
}
//-----------------------------------------------------------------------------------
-------

//-----------------------------------------------------------------------------------
-------
// 函数名:checkuser(&#36;c_user)
// 作 用:判断是否为合法用户名
// 参 数:&#36;c_user(待检测的用户名)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkuser(&#36;c_user)
{
if (!checklengthbetween(&#36;c_user, 4, 20)) return false; //宽度检验
if (!ereg(&quot;^[_a-za-z0-9]*&#36;&quot;, &#36;c_user)) return false; //特殊字符检验
return true;
}
//-----------------------------------------------------------------------------------
-------

//-----------------------------------------------------------------------------------
-------
// 函数名:checkpassword(&#36;c_passwd)
// 作 用:判断是否为合法用户密码
// 参 数:&#36;c_passwd(待检测的密码)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkpassword(&#36;c_passwd)
{
if (!checklengthbetween(&#36;c_passwd, 4, 20)) return false; //宽度检测
if (!ereg(&quot;^[_a-za-z0-9]*&#36;&quot;, &#36;c_passwd)) return false; //特殊字符检测
return true;
}
//-----------------------------------------------------------------------------------
-------

//-----------------------------------------------------------------------------------
-------
// 函数名:checktelephone(&#36;c_telephone)
// 作 用:判断是否为合法电话号码
// 参 数:&#36;c_telephone(待检测的电话号码)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checktelephone(&#36;c_telephone)
{
if (!ereg(&quot;^[+]?[0-9]+([xx-][0-9]+)*&#36;&quot;, &#36;c_telephone)) return false;
return true;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:checkvaluebetween(&#36;n_var, &#36;n_val1, &#36;n_val2)
// 作 用:判断是否是某一范围内的合法值
// 参 数:&#36;n_var 待检测的值
// &#36;n_var1 待检测值的上限
// &#36;n_var2 待检测值的下限
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkvaluebetween(&#36;n_var, &#36;n_val1, &#36;n_val2)
{
if (&#36;n_var < &#36;n_var1 ││ &#36;n_var > &#36;n_var2)
{
return false;
}
return true;

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


//-----------------------------------------------------------------------------------
-------
// 函数名:checkpost(&#36;c_post)
// 作 用:判断是否为合法邮编(固定长度)
// 参 数:&#36;c_post(待check的邮政编码)
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkpost(&#36;c_post)
{
&#36;c_post=trim(&#36;c_post);
if (strlen(&#36;c_post) == 6)
{
if(!ereg(&quot;^[+]?[_0-9]*&#36;&quot;,&#36;c_post))
{
return true;;
}else
{
return false;
}
}else
{
return false;;
}
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:checkextendname(&#36;c_filename,&#36;a_extend)
// 作 用:上传文件的扩展名判断
// 参 数:&#36;c_filename 上传的文件名
// &#36;a_extend 要求的扩展名
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkextendname(&#36;c_filename,&#36;a_extend)
{
if(strlen(trim(&#36;c_filename)) < 5)
{
return 0; //返回0表示没上传图片
}
&#36;lastdot = strrpos(&#36;c_filename, &quot;.&quot;); //取出.最后出现的位置
&#36;extended = substr(&#36;c_filename, &#36;lastdot+1); //取出扩展名

for(&#36;i=0;&#36;i<count(&#36;a_extend);&#36;i++) //进行检测
{
if (trim(strtolower(&#36;extended)) == trim(strtolower(&#36;a_extend[&#36;i]))) //转换大
小写并检测
{
&#36;flag=1; //加成功标志
&#36;i=count(&#36;a_extend); //检测到了便停止检测
}
}

if(&#36;flag<>1)
{
for(&#36;j=0;&#36;j<count(&#36;a_extend);&#36;j++) //列出允许上传的扩展名种类
{
&#36;alarm .= &#36;a_extend[&#36;j].&quot; &quot;;
}
alertexit(&#39;只能上传&#39;.&#36;alarm.&#39;文件!而你上传的是&#39;.&#36;extended.&#39;类型的文件&#39;);
return -1; //返回-1表示上传图片的类型不符
}

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


//-----------------------------------------------------------------------------------
-------
// 函数名:checkimagesize(&#36;imagefilename,&#36;limitsize)
// 作 用:检验上传图片的大小
// 参 数:&#36;imagefilename 上传的图片名
// &#36;limitsize 要求的尺寸
// 返回值:布尔值
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function checkimagesize(&#36;imagefilename,&#36;limitsize)
{
&#36;size=getimagesize(&#36;imagefilename);
if (&#36;size[0]>&#36;limitsize[0] ││ &#36;size[1]>&#36;limitsize[1])
{
alertexit(&#39;图片尺寸过大&#39;);
return false;
}
return true;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:alert(&#36;c_alert,&#36;i_goback=0)
// 作 用:非法操作警告
// 参 数:&#36;c_alert(提示的错误信息)
// &#36;i_goback(返回到那一页)
// 返回值:字符串
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function alert(&#36;c_alert,&#36;i_goback=0)
{
if(&#36;i_goback<>0)
{
echo &quot;<script>alert(&#39;&#36;c_alert&#39;);history.go(&#36;i_goback);</script>&quot;;
}
else
{
echo &quot;<script>alert(&#39;&#36;c_alert&#39;);</script>&quot;;
}
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
//-----------------------------------------------------------------------------------
-------
// 函数名:alertexit(&#36;c_alert,&#36;i_goback=0)
// 作 用:非法操作警告
// 参 数:&#36;c_alert(提示的错误信息)
// &#36;i_goback(返回到那一页)
// 返回值:字符串
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function alertexit(&#36;c_alert,&#36;i_goback=0)
{
if(&#36;i_goback<>0)
{
echo &quot;<script>alert(&#39;&#36;c_alert&#39;);history.go(&#36;i_goback);</script>&quot;;
exit;
}
else
{
echo &quot;<script>alert(&#39;&#36;c_alert&#39;);</script>&quot;;
exit;
}
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:replacespacialchar(&#36;c_char)
// 作 用:特殊字符替换函数
// 参 数:&#36;c_char(待替换的字符串)
// 返回值:字符串
// 备 注:无
//-----------------------------------------------------------------------------------
-------
function replacespecialchar(&#36;c_char)
{
&#36;c_char=htmlspecialchars(&#36;c_char); //将特殊字元转成 html 格式。
&#36;c_char=nl2br(&#36;c_char); //将回车替换为<br>
&#36;c_char=str_replace(&quot; &quot;,&quot; &quot;,&#36;c_char); //替换空格替换为
&#36;c_char=str_replace(&quot;<? &quot;,&quot;< ?&quot;,&#36;c_char); //替换php标记
return &#36;c_char;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
-------
// 函数名:exchangemoney(&#36;n_money)
// 作 用:资金转换函数
// 参 数:&#36;n_money(待转换的金额数字)
// 返回值:字符串
// 备 注:本函数示例:&#36;char=exchangemoney(5645132.3155) ==>
&#36;char=&#39;¥5,645,132.31&#39;
//-----------------------------------------------------------------------------------
-------
function exchangemoney(&#36;n_money)
{
&#36;a_tmp=explode(&quot;.&quot;,&#36;n_money ); //将数字按小数点分成两部分,并存入数组&#36;a_tmp
&#36;i_len=strlen(&#36;a_tmp[0]); //测出小数点前面位数的宽度

if(&#36;i_len%3==0)
{
&#36;i_step=&#36;i_len/3; //如前面位数的宽度mod 3 = 0 ,可按,分成&#36;i_step
部分
}else
{
&#36;step=(&#36;len-&#36;len%3)/3+1; //如前面位数的宽度mod 3 != 0 ,可按,分成&#36;i_step
部分+1
}

&#36;c_cur=&quot;&quot;;
//对小数点以前的金额数字进行转换
while(&#36;i_len<>0)
{
&#36;i_step--;

if(&#36;i_step==0)
{
&#36;c_cur .= substr(&#36;a_tmp[0],0,&#36;i_len-(&#36;i_step)*3);
}else
{
&#36;c_cur .= substr(&#36;a_tmp[0],0,&#36;i_len-(&#36;i_step)*3).&quot;,&quot;;
}

&#36;a_tmp[0]=substr(&#36;a_tmp[0],&#36;i_len-(&#36;i_step)*3);
&#36;i_len=strlen(&#36;a_tmp[0]);
}

//对小数点后面的金额的进行转换
if(&#36;a_tmp[1]==&quot;&quot;)
{
&#36;c_cur .= &quot;.00&quot;;
}else
{
&#36;i_len=strlen(&#36;a_tmp[1]);
if(&#36;i_len<2)
{
&#36;c_cur .= &quot;.&quot;.&#36;a_tmp[1].&quot;0&quot;;
}else
{
&#36;c_cur .= &quot;.&quot;.substr(&#36;a_tmp[1],0,2);
}
}

//加上人民币符号并传出
&#36;c_cur=&quot;¥&quot;.&#36;c_cur;
return &#36;c_cur;
}
//-----------------------------------------------------------------------------------
-------


//-----------------------------------------------------------------------------------
------
// 函数名:windowlocation(&#36;c_url,&#36;c_get=&quot;&quot;,&#36;c_getother=&quot;&quot;)
// 作 用:php中的window.location函数
// 参 数:&#36;c_url 转向窗口的url
// &#36;c_get get方法参数
// &#36;c_getother get方法的其他参数
// 返回值: 字符串
// 备 注:无
//-----------------------------------------------------------------------------------
-----
function windowlocation(&#36;c_url,&#36;c_get=&quot;&quot;,&#36;c_getother=&quot;&quot;)
{
if(&#36;c_get == &quot;&quot; && &#36;c_getother == &quot;&quot;)
if(&#36;c_get == &quot;&quot; && &#36;c_getother <> &quot;&quot;){&#36;c_target=&quot;&quot;window.location=&#39;&#36;c_url?
&#36;c_getother=&#39;+this.value&quot;&quot;;}
if(&#36;c_get <> &quot;&quot; && &#36;c_getother == &quot;&quot;){&#36;c_target=&quot;&quot;window.location=&#39;&#36;c_url?
&#36;c_get&#39;&quot;&quot;;}
if(&#36;c_get <> &quot;&quot; && &#36;c_getother <> &quot;&quot;){&#36;c_target=&quot;&quot;window.location=&#39;&#36;c_url?
&#36;c_get&&#36;c_getother=&#39;+this.value&quot;&quot;;}
return &#36;c_target;
}
//-----------------------------------------------------------------------------------
-----

?>  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons