Search email in text string

0

I'm looking for the world if you could search for an email address in a text string or text string (I do not know exactly what difference there is) and how you could get only that address to use it as a variable.

I had thought about using strrpos and derivatives but of course being a variable address for each case it is not How could I use it, since it seems that I have to put the full text.

The idea is to remove it from a textarea that the user fills in.

I would like to do it in php, or jQuery although I would prefer to do it in php.

Is there any way to do this or do I have to put an input to put the exclusive mail?

    
asked by Killpe 11.06.2017 в 19:45
source

2 answers

4

Sorry for my Spanish, I am not a native speaker, but I will try to explain a possible solution to your problem.

The problem in this case is that users can write whatever they want in textarea . That's why you can not make sure that your code will find the email. I advise you to use an input for each type of data that you need to use directly in the code.

If you still want to find the email, here is a method to do so in php with preg_match ( string $pattern , string $subject, array $matches ) . It is a function that allows you to find text based on regular expressions. Documentation: link

Here the $pattern will be the regular expression, in your case you can use $pattern = '/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i'; . $subject is the variable with the text that was in textarea and $matches is the variable that will contain the emails found in $subject .

Example

$pattern = '/[a-z\d._%+-]+@[a-z\d.-]+\.[a-z]{2,4}\b/i';
$subject = "Hola, me llamo Juan y mi correo electrónico es [email protected]. Saludos!";
preg_match ( $pattern, $subject, $matches );

print_r($matches);
/*
Array
(
    [0] => [email protected]
)
*/

More information

  • A similar question in English: link
  • Regular expressions: link
answered by 11.06.2017 / 20:46
source
1

Finding mails is complicated, but the RFX defined in RFC5322 is the most accurate you can use without using frameworks.

$pattern = '/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD'

Once the pattern is obtained you can find the groups with:

preg_match_all($pattern,$string_a_buscar,$array_matches)
    
answered by 12.06.2017 в 01:21