Just add _
in regex, another detail is that when using hype inside []
do the "escape", also recommend adding [0-9]
(or d) because some emails have numbers, would be like this:
If you want to have underline/underscore in the "user/account name":
/([_\-.a-zA-Z\d]{1,30})@([\-.a-zA-Z\d]{1,30})([.]{1})([\-.a-zA-Z]{1,10})/
The code would look like this:
preg_match('/([_\-.a-zA-Z\d]{1,30})@([\-.a-zA-Z\d]{1,30})([.]{1})([\-.a-zA-Z]{1,10})/', 'asdas asdasd asdas [email protected] asdasd asdas asdas', $msgPreg1);
If you want to have underline/underscore in the "username/account" and domain:
/([_\-.a-zA-Z\d]{1,30})@([_\-.a-zA-Z\d]{1,30})([.]{1})([_\-.a-zA-Z]{1,10})/
The code would look like this:
preg_match('/([_\-.a-zA-Z\d]{1,30})@([_\-.a-zA-Z\d]{1,30})([.]{1})([_\-.a-zA-Z]{1,10})/', 'asdas asdasd asdas [email protected] asdasd asdas asdas', $msgPreg1);
Note: preg_match
will only return you an email (1 match), to catch more than one use preg_match_all
, see the difference in ideone:
the purpose is to extract emails from a text or validate an entry (sanitize, filter)?
– Daniel Omine