Regular.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /* 正则集合
  3. Created by wanghong<1204772286@qq.com>
  4. Date: 2021-02-24 */
  5. namespace utils;
  6. class Regular{
  7. /**
  8. * 判断是否示手机号
  9. */
  10. public static function is_phonenumber($str){
  11. $preg ="/^1[3456789]\d{9}$/";
  12. return preg_match($preg,$str) ? true : false;
  13. }
  14. //判断是否是邮箱
  15. public static function is_email($str){
  16. $preg='/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims';
  17. return preg_match($preg,$str) ? true : false;
  18. }
  19. // 判断手机号或者邮箱,1手机号,2邮箱,0不是
  20. public static function check_account($str){
  21. if(self::is_phonenumber($str)){
  22. return 1;
  23. }
  24. if(self::is_email($str)){
  25. return 2;
  26. }
  27. return 0;
  28. }
  29. //判断是否是网址
  30. public static function is_url($str){
  31. $preg='/^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/';
  32. return preg_match($preg,$str) ? true : false;
  33. }
  34. /**
  35. * 摘取手机号
  36. * @param string $oldStr
  37. * @return array
  38. */
  39. public static function findThePhoneNumbers($oldStr = "",$onlyone=true){
  40. // 检测字符串是否为空
  41. $oldStr=trim("q{$oldStr}q");
  42. if(empty($oldStr)){
  43. return false;
  44. }
  45. $strArr = explode("-", $oldStr);
  46. $newStr = $strArr[0];
  47. for ($i=1; $i < count($strArr); $i++) {
  48. if (preg_match("/\d{2}$/", $newStr) && preg_match("/^\d{11}/", $strArr[$i])){
  49. $newStr .= $strArr[$i];
  50. } elseif (preg_match("/\d{3,4}$/", $newStr) && preg_match("/^\d{7,8}/", $strArr[$i])) {
  51. $newStr .= $strArr[$i];
  52. } else {
  53. $newStr .= "-".$strArr[$i];
  54. }
  55. }
  56. // 手机号的获取
  57. $reg='/\D(?:86)?(\d{11})\D/is';//匹配数字的正则表达式
  58. preg_match_all($reg,$newStr,$result);
  59. $nums = array();
  60. $common = '/^1[3-9]\d{9}$/';
  61. foreach ($result[1] as $key => $value) {
  62. if(preg_match($common,$value)){
  63. $nums[] = $value;
  64. }
  65. }
  66. if(count($nums)>0){
  67. return $onlyone ? $nums[0] : $nums;
  68. }else{
  69. return false;
  70. }
  71. }
  72. // 验证身份证号码
  73. public static function is_idcard($str){
  74. $reg = '/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/i';
  75. return preg_match($reg,$str) ? true : false;
  76. }
  77. }