本文介绍了正则表达式拆分电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关php regex的帮助,我想将电子邮件地址 [email protected]拆分为 johndoe和 @ example.com

I need some help with php regex, I want to "split" email address "[email protected]" to "johndoe" and "@example.com"

直到现在我有了这个: preg_match('/<?([^<] +?)@ /','[email protected]',$ matches);
而我得到 Array([0] => johndoe @ [1] => johndoe)

Until now I have this: preg_match('/<?([^<]+?)@/', '[email protected]', $matches);And I get Array ( [0] => johndoe@ [1] => johndoe)

那么我该如何更改正则表达式?

So how I need to change regex?

推荐答案

$parts = explode('@', "[email protected]");

$user = $parts[0];
// Stick the @ back onto the domain since it was chopped off.
$domain = "@" . $parts[1];

这篇关于正则表达式拆分电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:21