1. <?php
  2. /**
  3. * 常用的正则表达式来验证信息.如:网址 邮箱 手机号等
  4. */
  5. class check {
  6. /**
  7. * 正则表达式验证email格式
  8. *
  9. * @param string $str    所要验证的邮箱地址
  10. * @return boolean
  11. */
  12. public static function isEmail($str) {
  13. if (!$str) {
  14. return false;
  15. }
  16. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $str) ? true : false;
  17. }
  18. /**
  19. * 正则表达式验证网址
  20. *
  21. * @param string $str    所要验证的网址
  22. * @return boolean
  23. */
  24. public static function isUrl($str) {
  25. if (!$str) {
  26. return false;
  27. }
  28. return preg_match('#(http|https|ftp|ftps)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?#i', $str) ? true : false;
  29. }
  30. /**
  31. * 验证字符串中是否含有汉字
  32. *
  33. * @param integer $string    所要验证的字符串。注:字符串编码仅支持UTF-8
  34. * @return boolean
  35. */
  36. public static function isChineseCharacter($string) {
  37. if (!$string) {
  38. return false;
  39. }
  40. return preg_match('~[\x{4e00}-\x{9fa5}]+~u', $string) ? true : false;
  41. }
  42. /**
  43. * 验证字符串中是否含有非法字符
  44. *
  45. * @param string $string    待验证的字符串
  46. * @return boolean
  47. */
  48. public static function isInvalidStr($string) {
  49. if (!$string) {
  50. return false;
  51. }
  52. return preg_match('#[!#$%^&*(){}~`"\';:?+=<>/\[\]]+#', $string) ? true : false;
  53. }
  54. /**
  55. * 用正则表达式验证邮证编码
  56. *
  57. * @param integer $num    所要验证的邮政编码
  58. * @return boolean
  59. */
  60. public static function isPostNum($num) {
  61. if (!$num) {
  62. return false;
  63. }
  64. return preg_match('#^[1-9][0-9]{5}$#', $num) ? true : false;
  65. }
  66. /**
  67. * 正则表达式验证身份证号码
  68. *
  69. * @param integer $num    所要验证的身份证号码
  70. * @return boolean
  71. */
  72. public static function isPersonalCard($num) {
  73. if (!$num) {
  74. return false;
  75. }
  76. return preg_match('#^[\d]{15}$|^[\d]{18}$#', $num) ? true : false;
  77. }
  78. /**
  79. * 正则表达式验证IP地址, 注:仅限IPv4
  80. *
  81. * @param string $str    所要验证的IP地址
  82. * @return boolean
  83. */
  84. public static function isIp($str) {
  85. if (!$str) {
  86. return false;
  87. }
  88. if (!preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $str)) {
  89. return false;
  90. }
  91. $ipArray = explode('.', $str);
  92. //真实的ip地址每个数字不能大于255(0-255)
  93. return ($ipArray[0]<=255 && $ipArray[1]<=255 && $ipArray[2]<=255 && $ipArray[3]<=255) ? true : false;
  94. }
  95. /**
  96. * 用正则表达式验证出版物的ISBN号
  97. *
  98. * @param integer $str    所要验证的ISBN号,通常是由13位数字构成
  99. * @return boolean
  100. */
  101. public static function isBookIsbn($str) {
  102. if (!$str) {
  103. return false;
  104. }
  105. return preg_match('#^978[\d]{10}$|^978-[\d]{10}$#', $str) ? true : false;
  106. }
  107. /**
  108. * 用正则表达式验证手机号码(中国大陆区)
  109. * @param integer $num    所要验证的手机号
  110. * @return boolean
  111. */
  112. public static function isMobile($num) {
  113. if (!$num) {
  114. return false;
  115. }
  116. return preg_match('#^13[\d]{9}$|14^[0-9]\d{8}|^15[0-9]\d{8}$|^18[0-9]\d{8}$#', $num) ? true : false;
  117. }
  118. /**
  119. * 检查字符串是否为空
  120. *
  121. * @access public
  122. * @param string $string 字符串内容
  123. * @return boolean
  124. */
  125. public static function isMust($string = null) {
  126. //参数分析
  127. if (is_null($string)) {
  128. return false;
  129. }
  130. return empty($string) ? false : true;
  131. }
  132. /**
  133. * 检查字符串长度
  134. *
  135. * @access public
  136. * @param string $string 字符串内容
  137. * @param integer $min 最小的字符串数
  138. * @param integer $max 最大的字符串数
  139. */
  140. public static function isLength($string = null, $min = 0, $max = 255) {
  141. //参数分析
  142. if (is_null($string)) {
  143. return false;
  144. }
  145. //获取字符串长度
  146. $length = strlen(trim($string));
  147. return (($length >= (int)$min) && ($length <= (int)$max)) ? true : false;
  148. }
  149. }

检查字符串长度 检查字符串是否为空 用正则表达式验证出版物的ISBN号 用正则表达式验证邮证编码 验证字符串中是否含有汉字的更多相关文章

  1. c c++怎么判断一个字符串中是否含有汉字

    c c++怎么判断一个字符串中是否含有汉字 (2013-02-05 10:44:23) 转载▼     #include  #include  int main() { char sztext[] = ...

  2. java判断字符串中是否含有汉字

    原文:http://www.open-open.com/code/view/1426332240717 判断字符串中是否含有汉字: String str = "test中文汉字"; ...

  3. c# 判断字符串中是否含有汉字,数字

    正则表达式使用时需要引用 using System.Text.RegularExpressions; private void buttonX1_Click(object sender, EventA ...

  4. php_match/preg_match_all 默认有字符串长度限制

    php_match/preg_match_all 默认有字符串长度限制:52500(或许你的服务器环境是更长,或者更短),当字符串长度大于52500,只能匹配到52500数据,超出的部分会被系统自己截 ...

  5. PHP获取中英文混合字符串长度及截取

    1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 *  ...

  6. Shell脚本中字符串判空:使用-z 字符串长度为0时,为真,-n字符串长度不为0,为真。这两个都不靠谱【转】

    最近发现使用  -z   和  -n  来判断字符串判空,或不空时,很不靠谱. 使用下面的方法最可靠: if [ "x${value}" == "x" ]    ...

  7. php中常用的字符串长度函数strlen()与mb_strlen()实例解释

    int strlen ( string $string )  int strlen ( string $string )  获取给定字符串的[字节]长度 成功则返回字符串$string的长度,如果$s ...

  8. ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度

    [卡法 常用js库]: js汇合 表单验证  cookie设置  日期格式  电话手机号码  email  整数  小数  金额   检查参数长度 // +---------------------- ...

  9. php 字符串长度函数

    php 字符串长度函数 php 字符串长度函数,在php测试字符串长度的函数有二个,一个是strlen,另一个是mb_strlen前一个默认是支持,后一个需要开启一个插件,下面我们来介绍一下二个函数的 ...

随机推荐

  1. 在tortoiseSVN上将trunk的代码merge到branch上去

    1.进入branch项目的目录 2.右键选择merge 3.下一步 4.选择trunk

  2. ibatis插入数据后返回自增长的主键

    insert into testTable ( activity_id,activity_title values ( #{activityId,jdbcType=INTEGER}, #{activi ...

  3. Python快速教程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 怎么能快速地掌握Python?这是和朋友闲聊时谈起的问题. Python包含的内容 ...

  4. RESTful Api 身份认证中的安全性设计探讨

    REST 是一种软件架构风格.RESTful Api 是基于 HTTP 协议的 Api,是无状态传输.它的核心是将所有的 Api 都理解为一个网络资源.将所有的客户端和服务器的状态转移(动作)封装到 ...

  5. feedback 是什么意思

    feedback 是什么意思 能否不要说 feedback 呢?  加一个 feedback?  天啊   先解释一下 feedback 是什么  ? 还有   aria-describedby=&q ...

  6. [SQL]LTRIM 、 RTRIM、SUBSTRING 如何使用

    (一) LTRIM ( character_expression )删除字符变量中的起始空格 RTRIM ( character_expression ) 删除字符变量中的尾随空格 (二) SUBST ...

  7. Sqlserver中存储过程,触发器,自定义函数(一)

    Sqlserver中存储过程,触发器,自定义函数 1.存储过程有关内容存储过程的定义:存储过程的分类:存储过程的创建,修改,执行:存储过程中参数的传递,返回与接收:存储过程的返回值:存储过程使用游标. ...

  8. 使用jaxp对比xml进行SAX解析

    package cn.itcast.sax; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcep ...

  9. Oracle GoldenGate 11.2 OGG-01168(转)

    为客户部署的Oracle GoldenGate在测试阶段出现如下的错误: 2012-04-24 10:45:20  ERROR   OGG-01168  Oracle GoldenGate Deliv ...

  10. python3 内置函数 filter()

    filter(function or None, iterable) --> filter object Return an iterator yielding those items of i ...