url email判断与转换函数的实例代码

发布时间:2020-01-17编辑:脚本学堂
用于检测判断url与email格式正确与否的函数,以及实现email、url转换的函数,有需要的朋友可以参考下。

完整代码如下:
 

复制代码 代码示例:

<?php
//email验证
function validateEmail($email)
{
return eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $email);
}

//url验证
function validateURL($url)
{
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);
}

//url转换
function convertURLS($text)
{
$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);
$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=""></a>", $text);
return $text;
}

//email转换
function convertMail($text)
{
$text = eregi_replace("([_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3}))", "<a href='mailto:'></a>", $text);
return $text;
}

function convertAllLinks($text)
{
$text = convertURLS($text);
$text = convertMail($text);
return $text;
}
?>