PHP將網(wǎng)址字符串轉(zhuǎn)換成超級(jí)鏈接

2010-08-28 10:56:04來(lái)源:西部e網(wǎng)作者:

PHP為 URL 地址預(yù)設(shè) http 字符串

有時(shí)需要接受一些表單中的網(wǎng)址輸入,但用戶很少添加 http:// 字段,此代碼將為網(wǎng)址添加該字段。
 
if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
   $_POST['url'] = 'http://'.$_POST['url'];
}


PHP將網(wǎng)址字符串轉(zhuǎn)換成超級(jí)鏈接

該函數(shù)將 URL 和 E-mail 地址字符串轉(zhuǎn)換為可點(diǎn)擊的超級(jí)鏈接。

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;
}

關(guān)鍵詞:PHP

贊助商鏈接: