php extract email address from string

Solutions on MaxInterview for php extract email address from string by the best coders in the world

showing results for - "php extract email address from string"
Cécile
02 May 2020
1<?php 
2    $string = 'Ruchika < ruchika@example.com >';
3    $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
4    preg_match_all($pattern, $string, $matches);
5    var_dump($matches[0]);
6?>
7
Camille
25 Mar 2017
1<?php
2function fetch_mails($text){
3    
4  //String that recognizes an e-mail
5    $str = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
6    preg_match_all($str, $text, $out);
7  //return a blank array if not true otherwise insert the email in $out and return
8    return isset($out[0]) ? $out[0] : array();
9}
10//string to be checked
11$tstr = '
12My first e-mail address is codespeedy@gmail.com
13My second e-mail address is code-speedy2019@yahoo.com
14Obviously this is not a emaail address: code__speedy@^gmail.co.in
15';
16//display email addresses
17echo"<pre>";
18print_r(fetch_mails($tstr));
19?>