验证电子邮件地址和URL?从javascript转换为PHP

时间:2011-09-19 10:08:47

标签: php javascript jquery regex jquery-validate

我从jquery.validate插件中提取了以下代码,这真的很难理解。我想将它们转换为PHP,最好能帮助我理解以下正则表达式代码的每一部分。

验证电子邮件地址:

  

/ ^((([AZ] | \ 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 \ 0x7F部分] | \ X21 | [\ x23- \ x5b] | [\ x5d- \ x7e] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(\([\ x01- \ X09 \ X0B \ X0C \ x0d- \ 0x7F部分] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0 - \ uFFEF]))))(((\ X20 | \ X09)(\ X0D \ X0A))(\ X20 | \ X09)+)(\ X22)))@(? (([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0 - \ uFFEF])([AZ] | \ d | - | | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])*([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF]))))+(([AZ] |。[\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(([AZ ] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])([AZ] | \ d | - | | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])*([AZ] |。?[\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])))$ / I

验证网址:

  

/ ^(HTTPS | FTP)://(((([AZ] | \ d | - | | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF] )|(%[\ DA-F] {2})| [\ $&安培;'()* +,=!] |:?)* @)(((\ d | [1-9] \ d | 1 \ d \ d | 2 [0-4] \ d | 25 [0-5])(\ d |。[1-9] \ d | 1 \ d \ d | 2 [0-4] \ d | 25 [0-5])(\ d |。[1-9] \ d | 1 \ d \ d | 2 [0-4] \ d | 25 [0-5])(\ d |。[1 -9] \ d | 1 \ d \ d | 2 [0-4] \ d | 25 [0-5]))|((([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])([AZ] | \ d | - | | _ | 〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])*([AZ] | \ d | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])))。 )+(([AZ] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(([AZ] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF] )([AZ] | \ d | - | | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])([AZ] | [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF]))))(:?\ d ))(/((([AZ] | \ d | - | | |〜| [ \ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(%[\ DA-F] {2})| [\ $&安培;'()* +,=] |:| @ )+(/(([AZ] | \ d | - | | |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(%[\ DA-F ] {2})| [\ $&安培;'()* +,=] |:????| @)的))(\((([AZ] | \ d | - | | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ UFD CF \ uFDF0- \ uFFEF])| [\ $&安培|(%[\ {2})DA-F];'()* +,=] |:| @)| [\ uE000- \ uF8FF] ?| / | \)*)(#((([AZ] | \ d | - ?| | _ |〜| [\ u00A0- \ uD7FF \ uF900- \ uFDCF \ uFDF0- \ uFFEF])|(% [\ DA-F] {2})| [\ $&安培;'()* +,=!] |:?| @)| / | \)*)$ / I

有一个地方特别让我感到困惑。不是(\ x22)字符B?为什么不说B?为什么0x22在电子邮件地址验证中很有用呢?

感谢。

更新:我将其修改为PHP代码,

  

$ pattern =“/ ^((([az] | \ d | [!#\ $%&'* + - / = \?\ ^ _ {\|}~]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_ {\ |}〜] | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])+))|((\ X22)(( ((\ X20 | \ X09)(\ X0D \ X0A))(\ X20 | \ X09)?+)((\ x01- \ X08 \ X0B \ X0C \ x0e- \ X1F \ 0x7F部分] | \ X21 | [\ x23- \ x5b] | [\ x5d- \ x7e] | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])|(\([\ x01- \ X09 \ X0B \ X0C \ x0d- \ 0x7F部分] | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}]))))(((\ X20 | \ X09)(\ X0D \ X0A))(\ X20 | \ X09)+)? (\ X22)))@((([AZ] | \ d | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF }])|(([AZ] | \ d | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])( [AZ] | \ d | - | | _ |〜| [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}] )*([AZ] | \ d |。[\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])))) +(([AZ] | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])|(([AZ] | [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])([AZ] | \ d | - | | _ | 〜| [\ X {00A0} - \ X {D7FF} \ X {F900} - \ X {FDCF} \ X {FDF0} - \ X {FFEF}])*([AZ] | [\ X {00A0} - \ X {D7FF} \ {X} F900 - \ X {FDCF} \ {X} FDF0 - \ X {} FFEF)))\ $ / U“。?;   preg_match($ pattern,“abc@def.com”);

它说,

警告:preg_match():编译失败:第7行xxx中偏移量406处的不匹配括号。

我无法分配括号错过匹配的位置。我尝试计算每个括号,看起来没问题。并且偏移406不包含括号。

2 个答案:

答案 0 :(得分:2)

使用Javascript函数并从那里复制表达式不是一个好的/安全的想法。据我所知,\ u和\ x是UTF-8转义码,它可能对JS很有用,但在你的PHP条件下没那么有用。

有很多很好的解决方案可以验证电子邮件地址和网址,以下是我从谷歌那里获得的一对。

电子邮件: http://www.linuxjournal.com/article/9585

URL: PHP validation/regex for URL

祝你好运,希望这有所帮助,是Shai。

答案 1 :(得分:1)

你的上一个陈述错了,\x22在ASCII和Unicode中都是"