php中邮箱email 电话等格式的验证
1.email验证函数,代码如下:
- function isValidEmail($email) {
- $email = strtolower($email);
- if (!preg_match(“/[^@]{1,64}@[^@]{1,255}/”, $email)) {
- return false;
- }
- $email_array = explode(“@”, $email);
- $local_array = explode(“.”, $email_array[0]);
- $length = sizeof($local_array);
- for ($i = 0; $i < $length; $i++) {
- if (!preg_match(“@^[a-z0-9_~-][a-z0-9_~.-]{0,63}$@”, $local_array[$i])) {
- return false;
- }
- }
- unset($length);
- if (!preg_match(“@^[?[0-9.]+]?$@”, $email_array[1])) {
- $domain_array = explode(“.”, $email_array[1]);
- $length = sizeof($domain_array);
- if ($length < 2) {
- return false;
- }
- for ($i = 0; $i < $length; $i++) {
- if (!preg_match(“/^(([a-z0-9][a-z0-9-]{0,61}[a-z0-9])|([a-z0-9]+))$/”, $domain_array[$i])) {
- return false;
- }
- }
- unset($length);
- }
- return true;
- }
2.验证移动电话是否符合规范,代码如下:
- function isValidCellPhone($cellPhone) {
- $flag = FALSE;
- if (preg_match(‘@^1[3458][0-9]{9}$@’, $cellPhone)) {
- $flag = TRUE;
- }
- return $flag;
- }
3.验证电话是否符合规范,代码如下:
- function isValidPhone($phone) {
- if (preg_match(‘@^1[3458][0-9]{9}$@’, $phone)) {
- return true;
- }
- if (preg_match(‘@((?:(?:(?:00860?|0)(?:10|2d|[3-9]dd))-?)?([2-8]d{6,7}))@’, $phone)){
- return true;
- }
- if (preg_match(‘@^([48]00(?:d{7}|-d{7}|-d{3}-d{4}|-d{4}-d{3}|d-d{3}-d{3}))$@’, $phone)) {
- return true;
- }
- return false;
- }