Home > Backend Development > PHP Tutorial > Extract email address from string using php regular expression_PHP tutorial

Extract email address from string using php regular expression_PHP tutorial

WBOY
Release: 2016-07-13 16:55:26
Original
1167 people have browsed it

Sometimes we have professional tools to collect content with email addresses on web pages on websites and extract the email addresses in the content. Let’s take a look at an example for reference if necessary.

[PHP]Code

The code is as follows
 代码如下 复制代码

function extract_emails($str){ 

    // This regular expression extracts all emails from a string: 

    $regexp = '/([a-z0-9_.-])+@(([a-z0-9-])+.)+([a-z0-9]{2,4})+/i'; 

    preg_match_all($regexp, $str, $m); 

 

    return isset($m[0]) ? $m[0] : array(); 

 

$test_string = 'This is a test string... 

 

        test1@example.org 

 

        Test different formats: 

        test2@example.org; 

        foobar 

         

 

        strange formats: 

        test5@example.org 

        test6[at]example.org 

        test7@example.net.org.com 

        test8@ example.org 

        test9@!foo!.org 

 

        foobar '; 

 print_r(extract_emails($test_string));

Copy code
function extract_emails($str){

// This regular expression extracts all emails from a string:

preg_match_all($regexp, $str, $m); return isset($m[0]) ? $m[0] : array(); }
$test_string = 'This is a test string...
test1@example.org Test different formats: test2@example.org;                                                                                                                                           strange formats: test5@example.org test6[at]example.org test7@example.net.org.com test8@example.org test9@!foo!.org foobar '; print_r(extract_emails($test_string)); http://www.bkjia.com/PHPjc/631676.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631676.htmlTechArticleSometimes we have professional tools to collect content with email addresses on the web pages of the website, and add the email address in the content The address is extracted. Let’s look at an example below. Please refer to it if necessary...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template