Email+URL的判断和自动转换函数

 1   
 2function validateEmail($email)   
 3{ return eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $email);   
 4}   
 5  
 6function validateURL($url)   
 7{ return eregi("^((ht|f)tp://)((([a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3}))|(([0-9]{1,3}\\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\\+=:\?\\.-]*)*)$", $url);   
 8}   
 9  
10function convertURLS($text)   
11{ $text = eregi_replace("((ht|f)tp://www\\.|www\\.)([a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})((/|\?)[a-z0-9~#%&\\\/'_\\+=:\?\\.-]*)*)", "http://www.\\\3", $text);   
12$text = eregi_replace("((ht|f)tp://)((([a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3}))|(([0-9]{1,3}\\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\\+=:\?\\.-]*)*)", "

<a href='"\\0"'>\\0</a>

1", $text);   
2return $text;   
3}   
4  
5function convertMail($text)   
6{ $text = eregi_replace("([_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3}))", "

<a href="mailto:\\0">\\0</a>

1", $text);   
2return $text;   
3}   
4  
5function convertAllLinks($text)   
6{ $text = convertURLS($text);   
7$text = convertMail($text);   
8return $text;   
9}   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus