Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式拆分电子邮件地址_Php_Regex_Email_Preg Match - Fatal编程技术网

Php 正则表达式拆分电子邮件地址

Php 正则表达式拆分电子邮件地址,php,regex,email,preg-match,Php,Regex,Email,Preg Match,我需要一些php正则表达式的帮助,我想“拆分”电子邮件地址johndoe@example.com“发送至”johndoe“和“@example.com” 到目前为止,我有这样一个:preg_match('/使用explode可能是这里最好的方法,但要使用regex执行此操作,您需要执行以下操作: /^([^@]*)(@.*)/ $email = 'a."b@c".d@e.f'; $parts = explode('@', $email); $user = $parts[0]; $domain

我需要一些php正则表达式的帮助,我想“拆分”电子邮件地址johndoe@example.com“发送至”johndoe“和“@example.com”


到目前为止,我有这样一个:
preg_match('/使用explode可能是这里最好的方法,但要使用regex执行此操作,您需要执行以下操作:

/^([^@]*)(@.*)/
$email = 'a."b@c".d@e.f';
$parts = explode('@', $email);
$user = $parts[0];
$domain = '@' . $parts[1];
^字符串的开头

([^@]*)任何非@符号($matches[0])的内容


(@.*)@符号后跟任何($matches[1])

如果您想要预匹配解决方案,也可以这样做

preg_match('/([^<]+)(@[^<]+)/','johndoe@example.com',$matches);

preg_match('/([^前面的一些答案是错误的,因为一个有效的电子邮件地址实际上可以包含多个@符号,因为它包含在点分隔的引号文本中。请参见以下示例:

$email = 'a."b@c".d@e.f';
echo (filter_var($email, FILTER_VALIDATE_EMAIL) ? 'V' : 'Inv'), 'alid email format.';
有效的电子邮件格式


可以存在多个分隔的文本块和多个@符号。这两个示例都是有效的电子邮件地址:

$email = 'a."b@c".d."@".e.f@g.h';
$email = '/."@@@@@@"./@a.b';

根据Michael Berkowski的爆炸式回答,此电子邮件地址如下所示:

/^([^@]*)(@.*)/
$email = 'a."b@c".d@e.f';
$parts = explode('@', $email);
$user = $parts[0];
$domain = '@' . $parts[1];
用户:a“b”
域名:@c.d


任何使用此解决方案的人都应该提防潜在的滥用。根据这些输出接受电子邮件地址,然后在数据库中插入$email可能会产生负面影响

$email = 'a."b@c".d@INSERT BAD STUFF HERE';

这些函数的内容只有在首先使用过滤器变量进行验证时才是准确的。

从左边开始: 这是一个简单的非正则表达式、非分解的解决方案,用于查找第一个不包含在分隔和引用文本中的@。嵌套分隔文本根据filter_var被视为无效,因此查找适当的@是一个非常简单的搜索

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $a = '"';
    $b = '.';
    $c = '@';
    $d = strlen($email);
    $contained = false;
    for($i = 0; $i < $d; ++$i) {
        if($contained) {
            if($email[$i] === $a && $email[$i + 1] === $b) {
                $contained = false;
                ++$i;
            }
        }
        elseif($email[$i] === $c)
            break;
        elseif($email[$i] === $b && $email[$i + 1] === $a) {
            $contained = true;
            ++$i;
        }
    }
    $local = substr($email, 0, $i);
    $domain = substr($email, $i);
}
数组([本地]=>a。“b@c.x.“@”.d.e[域]=>@f.g)

错误的电子邮件地址

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $domain = strrpos($email, '@');
    $local = substr($email, 0, $domain);
    $domain = substr($email, $domain);
}

右起: 在对filter_var进行了一些测试并研究了什么是可以接受的有效域名(用点分隔)之后,我创建了这个函数以获得更好的性能。在有效的电子邮件地址中,最后一个@应该是真的@,因为@符号永远不会出现在有效电子邮件地址的域中

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $domain = strrpos($email, '@');
    $local = substr($email, 0, $domain);
    $domain = substr($email, $domain);
}
作为一项功能:

function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $a = strrpos($email, '@');
    return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = explode('@', $email);
    $domain = '@' . array_pop($email);
    return array('local' => implode('@', $email), 'domain' => $domain);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = preg_split('/(.*)(@.*)$/', $email, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    return array('local' => $email[0], 'domain' => $email[1]);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    preg_match('/(.*)(@.*)$/', $email, $matches);
    return array('local' => $matches[1], 'domain' => $matches[2]);
}

或使用“爆炸”和“内爆”:

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $local = explode('@', $email);
    $domain = '@' . array_pop($local);
    $local = implode('@', $local);
}
作为一项功能:

function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $a = strrpos($email, '@');
    return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = explode('@', $email);
    $domain = '@' . array_pop($email);
    return array('local' => implode('@', $email), 'domain' => $domain);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = preg_split('/(.*)(@.*)$/', $email, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    return array('local' => $email[0], 'domain' => $email[1]);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    preg_match('/(.*)(@.*)$/', $email, $matches);
    return array('local' => $matches[1], 'domain' => $matches[2]);
}

如果您仍然希望使用正则表达式,则从有效电子邮件地址的末尾开始拆分字符串是最安全的选择

/(.*)(@.*)$/
(.*)匹配任何内容。
(@.*)匹配以@符号开头的任何内容。
$字符串的结尾

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $local = preg_split('/(.*)(@.*)$/', $email, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    $domain = $local[1];
    $local = $local[0];
}
作为一项功能:

function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $a = strrpos($email, '@');
    return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = explode('@', $email);
    $domain = '@' . array_pop($email);
    return array('local' => implode('@', $email), 'domain' => $domain);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = preg_split('/(.*)(@.*)$/', $email, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    return array('local' => $email[0], 'domain' => $email[1]);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    preg_match('/(.*)(@.*)$/', $email, $matches);
    return array('local' => $matches[1], 'domain' => $matches[2]);
}

作为一项功能:

function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $a = strrpos($email, '@');
    return array('local' => substr($email, 0, $a), 'domain' => substr($email, $a));
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = explode('@', $email);
    $domain = '@' . array_pop($email);
    return array('local' => implode('@', $email), 'domain' => $domain);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    $email = preg_split('/(.*)(@.*)$/', $email, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    return array('local' => $email[0], 'domain' => $email[1]);
}
function parse_email($email) {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
    preg_match('/(.*)(@.*)$/', $email, $matches);
    return array('local' => $matches[1], 'domain' => $matches[2]);
}
答复 这解决了Brogan的两种边缘情况(
a)。”b@c“.d.”@“。f@g.h
/。@@@@@@@/@a.b
)如中所示


由于存在多个“@”大小写,因此无效

我爱他,直到我读到他的最后一句话:

在有效电子邮件地址中,最后一个@应为真@,因为@符号不应出现在有效电子邮件地址的域中

if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $domain = strrpos($email, '@');
    $local = substr($email, 0, $domain);
    $domain = substr($email, $domain);
}

如果这是真的,他的答案似乎不必要的复杂。

使用正则表达式。例如:

$mailadress = "email@company.com";     
$exp_arr= preg_match_all("/(.*)@(.*)\.(.*)/",$mailadress,$newarr, PREG_SET_ORDER); 

/*
Array output:
Array
(
    [0] => Array
        (
            [0] => email@company.com
            [1] => email
            [2] => company
            [3] => com
        )

)
*/

我已经为此创建了一个通用正则表达式,并创建完整电子邮件、用户和域的命名捕获

Regex:

(?<email>(?<mailbox>(?:\w|[!#$%&'*+/=?^`{|}~-])+(?:\.(?:\w|[!#$%&'*+/=?^`{|}~-])+)*)@(?<full_domain>(?<subdomains>(?:(?:[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)\.)*)(?<root_domain>[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)\.(?<tld>[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)))
(?<email>                          #  start Full Email capture
  (?<mailbox>                      #    Mailbox
    (?:\w|[!#$%&'*+/=?^`{|}~-])+   #      letter, number, underscore, or any of these special characters
    (?:                            #      Group: allow . in the middle of mailbox; can have multiple but can't be consecutive (no john..smith)
      \.                           #        match "." 
      (?:\w|[!#$%&'*+/=?^`{|}~-])+ #        letter, number, underscore, or any of these special characters
    )*                             #      allow one letter mailboxes
  )                                #    close Mailbox capture
  @                                #    match "@"
  (?<full_domain>                  #    Full Domain (including subdomains and tld)
    (?<subdomains>                 #      All Subdomains
      (?:                          #        label + '.' (so we can allow 0 or more)
        (?:                        #          label text
          [^\W\d_]                 #            start with a letter (\W is the inverse of \w so we end up with \w minus numbers and _)
          (?:                      #            paired with a ? to allow single letter domains
            (?:[^\W_]|-)+          #              allow letters, numbers, hyphens, but not underscore
            [^\W_]                 #              if domain is more than one character, it has to end with a letter or digit (not a hyphen or underscore)
          )?                       #            allow one letter sub domains
        )                          #          end label text
      \.)*                         #        allow 0 or more subdomains separated by '.'
    )                              #      close All Subdomains capture
    (?<root_domain>                #      Root Domain
      [^\W\d_]                     #        start with a letter
      (?:                          #        paired with ? to make characters after the first optional
        (?:[^\W_]|-)+              #          allow letters, numbers, hyphens
        [^\W_]                     #          if domain is more than one character, it has to end with a letter or digit (not a hyphen or underscore)
      )?                           #        allow one letter domains
    )                              #      close Root Domain capture
    \.                             #      separator
    (?<tld>                        #      TLD
      [^\W\d_]                     #        start with a letter
      (?:                          #        paired with ? to make characters after the first optional
        (?:[^\W_]|-)+              #          allow letters, numbers, hyphens
        [^\W_]                     #          if domain is more than one character, it has to end with a letter or digit (not a hyphen)
      )?                           #        allow single letter tld
    )                              #      close TLD capture
  )                                #    close Full Domain capture
)                                  #  close Full Email capture
以下代码::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::(((::::::::)目前目前目前,若若无无任何任何任何人在在在在任何任何任何情况下,将将将将将将在任何任何任何任何情况下,在在在任何任何情况下进行进行进行进行任何任何任何任何任何工作,,,在在在在在任何任何情况下,在任何任何任何情况下,在在任何情况下,在任何情况下,除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了除了在在在在任何其他任何其他其他其他U3](?:(?:[^\w_33;]|-)+[^\w_33;]?))
说明:

(?<email>(?<mailbox>(?:\w|[!#$%&'*+/=?^`{|}~-])+(?:\.(?:\w|[!#$%&'*+/=?^`{|}~-])+)*)@(?<full_domain>(?<subdomains>(?:(?:[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)\.)*)(?<root_domain>[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)\.(?<tld>[^\W\d_](?:(?:[^\W_]|-)+[^\W_])?)))
(?<email>                          #  start Full Email capture
  (?<mailbox>                      #    Mailbox
    (?:\w|[!#$%&'*+/=?^`{|}~-])+   #      letter, number, underscore, or any of these special characters
    (?:                            #      Group: allow . in the middle of mailbox; can have multiple but can't be consecutive (no john..smith)
      \.                           #        match "." 
      (?:\w|[!#$%&'*+/=?^`{|}~-])+ #        letter, number, underscore, or any of these special characters
    )*                             #      allow one letter mailboxes
  )                                #    close Mailbox capture
  @                                #    match "@"
  (?<full_domain>                  #    Full Domain (including subdomains and tld)
    (?<subdomains>                 #      All Subdomains
      (?:                          #        label + '.' (so we can allow 0 or more)
        (?:                        #          label text
          [^\W\d_]                 #            start with a letter (\W is the inverse of \w so we end up with \w minus numbers and _)
          (?:                      #            paired with a ? to allow single letter domains
            (?:[^\W_]|-)+          #              allow letters, numbers, hyphens, but not underscore
            [^\W_]                 #              if domain is more than one character, it has to end with a letter or digit (not a hyphen or underscore)
          )?                       #            allow one letter sub domains
        )                          #          end label text
      \.)*                         #        allow 0 or more subdomains separated by '.'
    )                              #      close All Subdomains capture
    (?<root_domain>                #      Root Domain
      [^\W\d_]                     #        start with a letter
      (?:                          #        paired with ? to make characters after the first optional
        (?:[^\W_]|-)+              #          allow letters, numbers, hyphens
        [^\W_]                     #          if domain is more than one character, it has to end with a letter or digit (not a hyphen or underscore)
      )?                           #        allow one letter domains
    )                              #      close Root Domain capture
    \.                             #      separator
    (?<tld>                        #      TLD
      [^\W\d_]                     #        start with a letter
      (?:                          #        paired with ? to make characters after the first optional
        (?:[^\W_]|-)+              #          allow letters, numbers, hyphens
        [^\W_]                     #          if domain is more than one character, it has to end with a letter or digit (not a hyphen)
      )?                           #        allow single letter tld
    )                              #      close TLD capture
  )                                #    close Full Domain capture
)                                  #  close Full Email capture
(?#开始完整的电子邮件捕获
(?#邮箱
(?:\w |[!#$%&'*+/=?^`{|}-])+#字母、数字、下划线或任何这些特殊字符
(?):允许。在邮箱的中间;可以有多个但不能连续(没有约翰…史密斯)
\“匹配”
(?:\w |[!#$%&'*+/=?^`{|}-])+#字母、数字、下划线或任何这些特殊字符
)*#允许一个信函邮箱
)#关闭邮箱捕获
@#匹配“@”
(?#完整域(包括子域和tld)
(?#所有子域
(?:#label+'。。(因此我们可以允许0或更多)
(?:#标签文本
[^\W\d_35;以字母开头(\W是\W的倒数,因此我们以\W减数和35;结尾)
(?:#与?配对以允许单字母域
(?:[^\W_]|-)+#允许字母、数字、连字符,但不允许下划线
[^\W_]#如果域包含多个字符,则必须以字母或数字(而不是连字符或下划线)结尾
)?#允许一个字母的子域
)#结束标签文本
\)*#允许0个或多个子域以“.”分隔
)#关闭所有子域捕获
(?#根域
以字母开头
(?:#与?配对以使第一个字符后面的字符可选
(?:[^\W_]|-)+#允许字母、数字、连字符
[^\W_]#如果域包含多个字符,则必须以字母或数字(而不是连字符或下划线)结尾
)?#允许一个字母的域
)#关闭根域捕获
\.#分离器
(?#TLD
以字母开头