Make clickable link in text [duplicate]

This code will convert the address http://example.ru/usr/aboutin the text into a link like this <a href="http://example.ru/usr/about">example</a>. That is, the link leads to the correct resource, but the description is not formed correctly. Minuses (-) and possibly other characters are discarded for some reason. Please help me to make the link look like it should

$ctext = preg_replace( '/(http)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '<a target="_blank" rel="nofollow" href="\0">\4</a>', $ctext );

Here is a test of the code, not to be unfounded http://ideone.com/HGfnd


Answer 1, authority 100%

$link = ' http://apokalipsis-2012.ru/?search=%20&test    http://ya.ru ';
$link = preg_replace('/http(s)?:\/\/[^\s]+/', '<a href="$0">$0</a>', $link);
echo $link;

Or without http:

$link = ' http://apokalipsis-2012.ru/?search=%20&test    http://ya.ru ';
$link = preg_replace('/http(s)?:\/\/([^\s]+)/', '<a href="$0">$2</a>', $link);
echo $link;

Added

$link = preg_replace('/http(s)?:\/\/([a-z0-9\-\.]+)[^\s]*/i', '<a href="$0">$2</a>', $link);

Answer 2

$ctext = preg_replace( '@(http)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)[email protected]', '<a target="_blank" rel="nofollow" href="\0">\4</a>', $ctext );