How to Extract All Email Addresses from String in PHP?

Oct 21, 2021 . Admin



Hi Guys,

Today, In this example, I would like to share with you how to find all email addresses in string PHP.

So, mainly PHP find the email in the string, PHP finds all email addresses in the string, PHP check email string, how to extract email address from text string PHP, PHP extract emails from the string.

Here, I will give you a full example of PHP extracting emails from the string so follow mine below steps.

Example:
<?php 
  
    $string = 'This email is example@gmail.com and company email is company@yahoo.com';
  
    $pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
    preg_match_all($pattern, $string, $matches);
  
    print_r($matches['0']);

?>
Output
Array
(
    [0] => example@gmail.com
    [1] => company@yahoo.com
)

It will help you...

#PHP