2012-08-12 161 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"); 
?> 
相關問題