代码之家  ›  专栏  ›  技术社区  ›  Topher Fangio

要用正则表达式替换电子邮件头的名称部分中的句点吗?

  •  0
  • Topher Fangio  · 技术社区  · 14 年前

     Mr. Joe Nobody <jo_nobody@nowhere.com>
    

     Mr. Joe Nobody <joe_nobody@here.com>, Mrs. Jane Noone <jane_noone@there.com>

    有人知道用标准字符串操作或正则表达式在PHP中实现这一点的方法吗?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Vantomex    14 年前

    这个正则表达式模式应该在PHP中工作:

    \.(?=[^<]*<)

    替换模式:空格、下划线或无

    例如:

      $email = 'Mr. Joe Nobody <joe_nobody@here.com>';
      $email = preg_replace('/\.(?=[^<]*<)/', '_', $email);
    
        2
  •  1
  •   thinker--    14 年前

    如上所述,电子邮件系统确实不需要这样做。话虽如此,您可以从地址中删除句点,忽略“<。>”之间的任何内容,如下所示

    $a="Mr. Joe Nobody <joe_nobody@here.com>, Mrs. Jane Noone <jane_noone@there.com>";
    $b=preg_replace("/([^<.]*)(\.|(<.*?>))/", "$1$3",$a); 
    echo $b
    
        3
  •  1
  •   Wrikken    14 年前
    $result = array();
    foreach(imap_rfc822_parse_adrlist('Mr. Joe Nobody <joe_nobody@here.com>, Mrs. Jane Noone <jane_noone@there.com>','') as $address){
       $result[] = preg_replace('/\.\s?/',' ',$address->personal)
          .' <'.$address->mailbox
          .'@'.$address->host.'>';
    }
    echo implode(', ',$result);
    

    但我同意以太的评论,应该没有必要。