You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

127 lines
4.8 KiB

  1. <?php
  2. /**
  3. *
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 张帅
  12. * PhpStorm CheckData.php
  13. * Create By 2017/4/14 16:21 $
  14. */
  15. namespace common\models;
  16. class CheckData
  17. {
  18. /**
  19. * Function Description:检验数组元素
  20. * Function Name: checkArrayData
  21. * @param $array
  22. * @param array $key_array
  23. *
  24. * @return bool
  25. *
  26. * @author 张帅
  27. */
  28. public function checkArrayData($array, $key_array = [[['key', 'key'], 'type'], [['key', 'key'], 'type']])
  29. {
  30. foreach ($key_array as $key => $vel) {
  31. switch ($vel[1]) {
  32. //必有且不为空
  33. case 1:
  34. foreach ($vel[0] as $k => $v) {
  35. if (!isset($array[$v]) || empty($array[$v])){
  36. return false;
  37. }
  38. }
  39. break;
  40. //必有切不为空,0 且必须为数字
  41. case 2:
  42. foreach ($vel[0] as $k => $v) {
  43. if (!isset($array[$v]) || empty($array[$v]) || !is_numeric($array[$v])){
  44. return false;
  45. }
  46. }
  47. break;
  48. //不必有 有则比为数字
  49. case 3:
  50. foreach ($vel[0] as $k => $v) {
  51. if (isset($array[$v]) && !is_numeric($array[$v])){
  52. return false;
  53. }
  54. }
  55. break;
  56. //必有切不为空,0 且必须为整数
  57. case 4:
  58. foreach ($vel[0] as $k => $v) {
  59. if (!isset($array[$v]) || empty($array[$v]) || !is_int($array[$v]+0)){
  60. return false;
  61. }
  62. }
  63. break;
  64. //存在且数量>0
  65. case 5:
  66. foreach ($vel[0] as $k => $v) {
  67. if (!isset($array[$v]) || count($array[$v]) == 0){
  68. return false;
  69. }
  70. }
  71. break;
  72. //必须是有且是****-**-**
  73. case 6:
  74. foreach ($vel[0] as $k => $v) {
  75. if (!isset($array[$v]) || !preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/",$array[$v])){
  76. return false;
  77. }
  78. }
  79. break;
  80. //必须是有且是价格(最多二位小数)
  81. case 7:
  82. foreach ($vel[0] as $k => $v) {
  83. if (!isset($array[$v]) || !preg_match('/(^[1-9]\d*(\.\d{1,2})?$)|(^[0]{1}(\.\d{1,2})?$)/',$array[$v])){
  84. return false;
  85. }
  86. }
  87. break;
  88. //必须是有且是手机
  89. case 8:
  90. foreach ($vel[0] as $k => $v) {
  91. if (!isset($array[$v]) || !preg_match('/^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/',$array[$v])){
  92. return false;
  93. }
  94. }
  95. break;
  96. //必须是有且是邮箱
  97. case 9:
  98. foreach ($vel[0] as $k => $v) {
  99. if (!isset($array[$v]) || !preg_match('/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/',$array[$v])){
  100. return false;
  101. }
  102. }
  103. break;
  104. //必须是有且是身份证号(中国大陆)
  105. case 10:
  106. foreach ($vel[0] as $k => $v) {
  107. if (!isset($array[$v]) || !preg_match('/^\d{15,15}$|^\d{17,17}([0-9]|[Xx])$/',$array[$v])){
  108. return false;
  109. }
  110. }
  111. break;
  112. //必有且是标准时间格式09:02
  113. case 11:
  114. foreach ($vel[0] as $k => $v) {
  115. if (!isset($array[$v]) || !preg_match('/^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[1-5][0-9])$/',$array[$v])){
  116. return false;
  117. }
  118. }
  119. break;
  120. }
  121. }
  122. return true;
  123. }
  124. }