PHP為 URL 地址預設 http 字符串
有時需要接受一些表單中的網址輸入,但用戶很少添加 http:// 字段,此代碼將為網址添加該字段。
if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
$_POST['url'] = 'http://'.$_POST['url'];
}
PHP將網址字符串轉換成超級鏈接
該函數將 URL 和 E-mail 地址字符串轉換為可點擊的超級鏈接。
function makeClickableLinks($text) {
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)',
'<a href="\1">\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)',
'\1<a href="http://\2">\2</a>', $text);
$text = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',
'<a href="mailto:\1">\1</a>', $text);
return $text;
}