Home > php教程 > php手册 > php正则之从字符串中提取email地址

php正则之从字符串中提取email地址

WBOY
Release: 2016-06-13 10:00:30
Original
1982 people have browsed it

有时我们会有专业的工具去搜集网站中网页上有email地址的内容,把内容中的邮箱地址给提取出来,下面我们来看个实例,有需要的参考一下。

[PHP]代码

 代码如下 复制代码

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));

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template