Wednesday, August 29, 2012

Fungsi autolink

Berikut ini script hasil nemu di suatu server, hehe :

function autolink ($str){
$str = eregi_replace("([[:space:]])((f|ht)tps?:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1\\2", $str); //http
$str = eregi_replace("([[:space:]])(www
\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1\\2", $str); // www.
$str = eregi_replace("([[:space:]])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","\\1\\2", $str); // mail
$str = eregi_replace("^((f|ht)tp:\/\/[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1", $str); //http

$str = eregi_replace("^(www\.[a-z0-9~#%@\&:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "\\1", $str); // www.
$str = eregi_replace("^([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})","\\1", $str); // mail
return $str;
}


Eksekusi :



Fungsi eregi_replace() ini menghasilkan pesan warning yang lumayan mengganggu, sehingga saya modif aja jadi pake preg_replace() seperti berikut :

function autolink ($str){
$str = preg_replace("((f|ht)tp[s]?:\/\/[a-z0-9~#%@\&
:=?+\/\.,_-]+[a-z0-9~#%@\&=?+\/_.;-]+)", "$0", $str);
return $str;
}


Eksekusi :