比如说一个页面中有这样的内容风中有托玉镯的云,雾里看花。虚拟主机,很好、买主机联系:[email protected]
。有事联系 [email protected][email protected].=======================================
请问要如何取得里面的所有邮箱地址并输出

解决方案 »

  1.   

    $s = '风中有托玉镯的云,雾里看花。虚拟主机,很好、买主机联系:[email protected]
    。有事联系 [email protected][email protected].';$r = '#\w+@\w+\.(com|cn)#';
    preg_match_all($r, $s, $a);
    echo '<pre>';print_r($a[0]);exit;
    /*
    输出:Array
    (
        [0] => [email protected]
        [1] => [email protected]
        [2] => [email protected]
        [3] => [email protected]
    )
    */
      

  2.   

    2楼不错。终于知道preg_match_all的用法。不知道还有没有其他函数可用
      

  3.   

    <?php
    ...//假设包含邮箱字符串为$target
    preg_match_all ("/\W+([-+.]\w+)*@\w+)*\.\w+([-.]\w+)*/",$target,$matches);//在$target中查找所有邮箱地址,保存到数组matches
    for($i=0;$i<count($matches[0]);$i++)echo $matches[0][$i]."</br>";   //逐个输出matches中的邮箱地址
    ?>