正则表达式拆分电子邮件地址 [英] Regex split email address

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

问题描述

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

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

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

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

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

So how I need to change regex?

推荐答案

$parts = explode('@', "johndoe@example.com");

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆