Catching with preg_match email with PHP special characters

0

With this code below it does not handle underline emails.

Is it possible to modify it to take even underline and other special characters?

In the example below it returns the value "[email protected]" and the correct one would be "[email protected]"

preg_match('/([-.a-zA-Z]{1,30})@([-.a-zA-Z]{1,30})([.]{1})([-.a-zA-Z]{1,10})/', 'asdas asdasd asdas [email protected] asdasd asdas asdas', $msgPreg1);
    echo trim($msgPreg1[0]);
    
asked by anonymous 15.08.2016 / 01:46

1 answer

2

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:

15.08.2016 / 01:53