2012-08-12 115 views

回答

0

这个PHP函数从字符串中提取电子邮件地址,并把它变成一个可点击的链接:

function extractEmail($data) 
{ 
    if (preg_match("/[^@\s][email protected]([-a-z0-9]+\.)+[a-z]{2,}/i", $data, $email)) 
    { 
     $replacement = '<a href="mailto:' . $email[ 0 ] . '" target="_blank">' . $email[ 0 ] . '</a> '; 
     $data  = preg_replace("/[^@\s][email protected]([-a-z0-9]+\.)+[a-z]{2,}/i", $replacement, $data); 
    } 
    return $data; 
} 

要使用它创建PHP文件,并添加有一个测试条目(但不忘记将上面的功能在它):

<?php 
    echo extractEmail("Some text goes here before [email protected] and some text here goes after"); 
?> 
相关问题