How to Extract All Email Addresses from String in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hi,

This is a short guide on php find email in string. you can understand a concept of php find all email addresses in string. We will look at example of php check email string. you'll learn how to extract email address from text string php.

Here, i will give you simple example how to get email addresses from string content in php. so let's see both code and output as bellow:

Example:

index.php

<?php

$string = 'This email is hardik@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] => hardik@gmail.com

[1] => company@yahoo.com

)

i hope it can help you...

Tags :
Shares