简体   繁体   中英

Regular Expressions on PHP remove url from an email string

I'm uploading Tab Delimited text files exported from Excel, basically i'm getting all email fields on the document, by using a preg_match_all

preg_match_all("/([\s]*)[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i",$string,$emails);

In some cases emails are saved with a url attached to it like this: prefix.user@domain.comwww.domain.com

i need to strip only the email without the url that's afterwards the email address

how can i make this work using regular expressions??

List all possible domain names in last regexp group like this, including top-level domains by default.

[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)

You can read more about email validation here or read the related question here .

UPDATE

Expression conforming to RFC 2822 standard

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM