PHP - Função para encontrar links em texto

Eu tenho uma função que converte seqüências de caracteres como 'www.example.com' e 'http://example.com'em hiperlinks. Também lida com subdomínios, por exemplo "http://sub.example.com'

Mas falha com este -http: // www.example.com 'e gera o seguinte

<a href="http://<a href="http://www.chemica.ru">www.chemica.ru</a>">http://<a href="http://www.chemica.ru">www.chemica.ru</a></a>

Por favor, alguém pode ajudar? O problema é que 'http: //' e 'www'. estão juntos e ambos têm maneiras diferentes de converter.

function makeLinks($text){ 
 $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text); 
 $text = eregi_replace('(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="http://\\1">\\1</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; 
}

questionAnswers(3)

yourAnswerToTheQuestion