CheckParamService.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Services;
  3. use DateTime;
  4. class CheckParamService
  5. {
  6. public function __construct()
  7. {
  8. }
  9. public function LenMToN($str, $m, $n)
  10. {
  11. if (mb_strlen($str) >= $m && mb_strlen($str) <= $n) {
  12. return true;
  13. } else {
  14. return false;
  15. }
  16. }
  17. public function isascii($str)
  18. {
  19. if (mb_detect_encoding($str) == "ASCII") {
  20. return true;
  21. } else {
  22. return false;
  23. }
  24. }
  25. public function isengnum($str)
  26. {
  27. return (preg_match("/^[a-zA-Z0-9]+$/", $str) == 1);
  28. }
  29. public function isnum($str)
  30. {
  31. return (preg_match("/^[0-9]+$/", $str) == 1);
  32. }
  33. public function validateDate($date, $format = 'Y-m-d H:i:s')
  34. {
  35. $d = DateTime::createFromFormat($format, $date);
  36. return $d && $d->format($format) == $date;
  37. }
  38. public function RemoveChars($str, $chars)
  39. {
  40. $chars = str_split($chars, 1);
  41. for ($i = 0; $i < count($chars); $i++) $str = str_replace($chars[ $i ], "", $str);
  42. return $str;
  43. }
  44. public function isIdentity($id)
  45. {
  46. $id = strtoupper(trim($id));
  47. $alphabetTable = [
  48. 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16,
  49. 'H' => 17, 'I' => 34, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22,
  50. 'O' => 35, 'P' => 23, 'Q' => 24, 'R' => 25, 'S' => 26, 'T' => 27, 'U' => 28,
  51. 'V' => 29, 'X' => 30, 'Y' => 31, 'Z' => 33
  52. ];
  53. if (!preg_match("/^[A-Z]{1}[12ABCD]{1}[0-9]{8}$/", $id)) {
  54. return false;
  55. }
  56. $idArray = str_split($id);
  57. $alphabet = $alphabetTable[ $idArray[0] ];
  58. $point = substr($alphabet, 0, 1) * 1 + substr($alphabet, 1, 1) * 9;
  59. for ($i = 1; $i <= 8; $i++) {
  60. $point += $idArray[ $i ] * (9 - $i);
  61. }
  62. $point = $point + $idArray[9];
  63. return $point % 10 == 0 ? true : false;
  64. }
  65. }