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. OAF_架构MVC系列1 - MVC的概述(概念)

     2015-04-03 Created By BaoXinjian

  2. HDU 5428 The Factor

    话说这题意真的是好难懂啊,尽管搜到了中文题意,然而还是没懂,最后看到了一个题解才懂的.http://www.cnblogs.com/Apro/p/4784808.html#3470972 题意:给出n ...

  3. 转--Invalidate和postInvalidate的更新view区别

    Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...

  4. JAVA 数组算法(复制、查找、插入)

    一.复制数组算法 //数组复制算法 public class Test{ public static void main(String[] args){ int[] arrA = {100,800,5 ...

  5. chkconfig : No such file or directory

    sys_version:12.04LTS For example: #chkconfig --level mysql on /sbin/insserv:No such file or director ...

  6. webim-界面细节调整

    1)左侧css调整 3)界面和滚动条美化 8)界面

  7. SQLSERVER:计算数据库中各个表的数据量和每行记录所占用空间

    转:http://www.cnblogs.com/lyhabc/p/3828496.html CREATE TABLE #tablespaceinfo ( nameinfo ) , rowsinfo ...

  8. (medium)LeetCode 207.Course Schedule

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  9. HDU 1043 八数码 Eight A*算法

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. <iOS>other linker flags[转]

    包含静态库时候需要在Target的Other linker flags里面加上值:-objC,-all_load,-force_load 对于64位机子和iPhone OS应用 解决方法是使用-all ...