代码之家  ›  专栏  ›  技术社区  ›  Udders

有人能解释一下这个剧本的作用吗

  •  0
  • Udders  · 技术社区  · 15 年前
    class person {
    
        var $name;
        var $email;
    
        //Getters
        function get_name() { return $this->name; }
        function get_email() { return $this->email; }
    
        //Setters
        function set_name( $name ) { $this->name = $name; }
    
        function set_email( $email ) {
    
            if ( !eregi("^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$", $email ) ) {
                return false;
            } else { 
                $this->email = $email;
                return true;
            }
    
        }//EOM set_email
    
    }//EOC person
    
    2 回复  |  直到 15 年前
        1
  •  9
  •   Paul Dixon    15 年前

    它是一个存储用户名和电子邮件地址的类。set_email()方法在存储前检查提供的地址,以确保它看起来有效。

    这个 eregi 函数使用正则表达式检查电子邮件地址。这些是执行字符串操作和解析的非常强大的方法,但是这个特定的示例可能不是最好的介绍。如果你刚开始使用正则表达式,你可能想看看 Perl compatible regular expressions 因为它们被更广泛地使用并且更强大。此外, ereg functions will be deprecated from PHP5.3+

    这里是 one source of introductory information ,我建议使用类似 Regex Coach 用于玩游戏和测试正则表达式。

    要分解它:

    ^                         # force match to be at start of string
    ([0-9,a-z,A-Z]+)          # one or more alphanumerics
    ([.,_,-]([0-9,a-z,A-Z]+)) # followed by period, underscore or 
                              # dash, and more alphanumerics
    *                         # last pattern can be repeated zero or more times
    [@]                       # followed by an @ symbol
    ([0-9,a-z,A-Z]+)          # and one or more alphanumerics
    ([.,_,-]([0-9,a-z,A-Z]+)) # followed by period, underscore or dash, 
                              # and more alphanumerics
    *                         # last pattern can be repeated zero or more times
    [.]                       # followed by a period
    ([0-9,a-z,A-Z]){2}        # exactly two alphanumerics
    ([0-9,a-z,A-Z])*$         # then the remainder of the string must be 
                              # alphanumerics too - the $ matches the end of the
                              # string
    

    编写一个regex来匹配所有有效电子邮件地址的100%是相当复杂的,这是一个简化的模式,可以匹配大多数电子邮件地址。这是一篇关于 writing email address regex patterns .

        2
  •  10
  •   Kimble    15 年前

    它是一个数据类,用于存储关于一个人的信息。它还可以验证电子邮件。如果给set_email方法一个无效的电子邮件(在本例中是一个与regex不匹配的字符串),该方法将返回false。