Just add _
to regex, another detail is that when using hife within []
make the "escape", I also recommend adding [0-9]
(or \ d) because some emails have numbers, it would look like this: / p>
If you want to have underline / underscore in the "user name / account":
/([_\-.a-zA-Z\d]{1,30})@([\-.a-zA-Z\d]{1,30})([.]{1})([\-.a-zA-Z]{1,10})/
The code looks 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 "user name / account" and domain:
/([_\-.a-zA-Z\d]{1,30})@([_\-.a-zA-Z\d]{1,30})([.]{1})([_\-.a-zA-Z]{1,10})/
The code looks 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 get more than one use preg_match_all
, see the difference in ideone: