0

好的,所以我有这个大的正则表达式来检查javascript中的电子邮件:

/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i

我想用 preg_match 在 php中使用它

但我得到这个错误:

编译失败:PCRE 不支持 some.php 中偏移量 44 处的 \L、\l、\N{name}、\U 或 \u

所以问题是\u字符所以如何将此正则表达式转换为 PHP,这样即使浏览器禁用了 javascript,我也可以检查电子邮件。所以\u00A0是我在某处看到的一些 UTF-8 字符,我必须转义反斜杠,但即使所有转义的反斜杠错误仍然相同,所以请帮我转换这个正则表达式

4

1 回答 1

1

如果您想要一个真正可靠地检查有效电子邮件的 PCRE 表达式,这是我所知道的唯一一个或多或少有效的表达式。在这里找到它

(?:(?:\r\n)?[ \t]) (?:(?:(?:[^()<>@,;:\".[] \000-\031]+(? :(?:(?:\r\n)?[ \t] )+|\Z|(?=[["()<>@,;:\".[]]))|"(?: [^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?: \r\n)?[ \t]) )( ?:.(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(? :( ?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|"(?:[^\ "\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)?[ \t]) )) @(? :(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\0 31]+(?:(?:(?: \r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\r\]| \.) \ ](?:(?:\r\n)?[ \t]) )(?:.(?:(?:\r\n)?[ \t]) (?:[^( )<>@,;:\".[] \000-\031]+ (?:(?:(?:\r\n)?[ \t])+|\Z|(?=[[" ()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?: (?:\r\n)?[ \t]) ))|(?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[["()<>@,;:\".[]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n )?[ \t])) "(?:(?:\r\n) ?[ \t]) )*\<(?:(?:\r\n)?[ \t]) (?: @(?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n) ?[ \t]) )(?:.(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\ 031]+(?:(?:(?:\r\n) ?[ \t])+|\Z|(?=[["()<>@,;:\".[]])) |[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t] ) )) (?:,@(?:(?:\r\n )?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t ])+|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(? :\r\n)?[ \t]) )(?:.(?:(?:\r\n)?[ \t])(?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t] )+|\Z| (?=[["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)? [ \t]) )) ) :(?:(?:\r\n)?[ \t]) )?(?:[^()<>@,;:\".[] \000-\ 031]+(?:(?:(?:\r\n)?[ \t])+ |\Z|(?=[["()<>@,;:\".[]])) |"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r \n)?[ \ t]) )(?:.(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+ (?:(?:(?: \r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|"( ?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t ])) "(?:(?:\r\n)?[ \t]) )) @(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031 ]+(?:(? :(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\ r\]|\.) ]( ?:(?:\r\n)?[ \t]) (?:.(?:(?:\r\n)?[ \t])(?:[^()<>@,;:\".[] \000-\031]+(? :(?:(?:\r\n)?[ \t])+|\Z| (?=[["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)? [ \t]) )) >(?:(?:\r\n)?[ \t]) )|(?:[^()<>@,;:\".[] \000-\031 ]+(?:(? :(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))| "(?:[^\"\r\]|\.|(?:(?:\r\n)? [ \t])) "(?:(?:\r\n)?[ \t ]) )*:(?:(?:\r\n)?[ \t]) (?:(?:(?:[^()<>@,;:\".[] \000-\ 031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]])) |"(?:[^\"\r\]| \.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)?[ \ t]) )(?:.(?:(?:\r\n)?[ \t]) (?:[^()<> @,;:\".[] \000-\031]+ (?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|" ( ?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)?[ \t]) )) @(?:(?:\r\n)?[ \t] )(?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z| (?=[["()<>@,;:\ ".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)? [ \t]) )(?:.(?:(?:\r\n)?[ \t]) (? :[^()<>@,;:\".[] \000-\031 ]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[ ]]))| [([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )) |(?:[^()<>@,;:\" .[] \000- \031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\ ".[]]))|"(?:[^\"\r\]|\.|( ?:(?:\r\n)?[ \t])) "(?:(?:\ r\n)?[ \t]) )*\<(?:(?:\r\n)?[ \t]) (?:@(?:[^()<>@,; :\" .[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\ ".[]]))|[([ ^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )(?:.(?:(? :\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\ ".[]]))|[([^[\]\r\]|\.)](?:(?:\r\n)?[ \t]) )) (?:,@(?:(?:\r\n)?[ \t]) (?:[^()< >@,;:\".\ [] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["( )<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )( ?:.(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(? :(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\ r\] |\.) ](?:(?:\r\n)?[ \t]) )) )*:(?:(?:\r\n)?[ \t]) )?( ?:[^()<>@,;:\".[] \0 00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z| (?=[["()<>@,;:\".[]]))|"(?:[^\"\r\]|\ .|(?:(?:\r\n) ?[ \t])) "(?:(?:\r\n)?[ \t]) )(?:.(?:(?:\r\n)?[ \t]) (?: [^()<>@, ;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?= [["()<>@,;:\".[]]))|"(? :[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)?[ \t] ) ))@(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(?:( ?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\r\ ]|\.) ](?:(?:\r\n)?[ \t]) )(?:.(?:(?:\r\n)?[ \t]) (?:[ ^ ()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[[ "()<>@,;:\".[] ]))|[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )) >(?:(?:\r\n)?[ \t]) )(?:,\s ( ?:(?:[^()<>@,;:\".[] \000 -\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\".[]] ))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)? [ \t]) )(?:.(?:( ?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031 ]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[ ["()<>@,;:\".[]]))| "(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?:\r\n)?[ \t ]) )) @(?:(?:\r\n)?[ \t])(?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t ])+|\Z| (?=[["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)? [ \t]) )(? :.(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031 ]+(?:(?:(?:\r\n)?[ \t])+| \Z|(?=[["()<>@,;:\".[]]))| [([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )) |(?: [^()<>@,;:\" .[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\ ".[\]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n)?[ \t])) "(?:(?: \r\n)?[ \t]) ) \<(?:(?:\r\n) ?[ \t]) (?:@(?:[^()<>@,;:\" .[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[[" ()<>@,;:\ ".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )(?:.(?:(? :\r\n) ?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["() <> @,;:\".[]]))|[([^[]\r\]|\.)](?:(?:\r\n)?[ \t]) )) (?:,@(?:(?:\r\n)?[ \t]) (?:[^()< >@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[["() <>@, ;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t]) )(? :.(?:(?:\r\n)?[ \t] ) (?:[^()<>@,;:\".[] \000-\031]+(?:(?: (?:\r\n)?[ \t])+|\Z|(?=[["()<>@,;:\ ".[]]))|[([^[]\r \]|\.) ](?:(?:\r\n)?[ \t]) )) )*:(?:(?:\r\n)?[ \t]) )? (?:[^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z| (?=[["()<>@,;:\".[]]))|"(?:[^\"\r\]|\.|(?:(?:\r\n) ?[ \t])) "(?:(?:\r\n)?[ \t]) )(?:.(?:(?: \r\n)?[ \t]) (?: [^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?= [[ "()<>@,;:\".[]]))|" @(?:(?:\r\n)?[ \t]) (?:[^()<>@,;:\".[] \000-\031]+(?:(?:( ?:\r\n)?[ \t]) +|\Z|(?=[["()<>@,;:\".[]]))|[([^[]\r\ ]|\.) ](?:(?:\r\n)?[ \t]) )(?:\ .(?:(?:\r\n)?[ \t]) (?:[ ^()<>@,;:\".[] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z |(?=[ ["()<>@,;:\".[]]))|[([^[]\r\]|\.) ](?:(?:\r\n)?[ \t] ) )) >(?:( ?:\r\n)?[ \t]) )) )?;\s )

但是,如果您想保持清醒:

if (filter_var($input, FILTER_VALIDATE_EMAIL))
{
}

可能更具可读性,而且通常更好:)

于 2013-07-29T08:38:24.170 回答