How to extract all email addresses from string in php?
In this post, I will give you simple example how to get email addresses from string content in php.
1. Code:
<?php function getAllEmailFromString($string){ $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i'; preg_match_all($pattern, $string, $matches); return $matches['0']; } $string = 'This email is soltuts@gmail.com and company email is soltuts@yahoo.com'; print_r(getAllEmailFromString($string)); ?>
2. Example:
Input:
String: 'This email is soltuts@gmail.com and company email is soltuts@yahoo.com'
Output:
Array ( [0] => soltuts@gmail.com [1] => soltuts@yahoo.com )