Finding links in a string

success!!!
If anyone is interested, for the moment it works in ANY case!

function make_clickable($text)
{
	$text = str_replace(array('http://','?'),array('','qmark'),$text);
	if (preg_match_all('~\\b([a-z0-9-]+\\.)+[^\\s]+\\b~si', $text, $urls))
	{
		foreach (array_unique($urls[0]) AS $url)
		{
			$urltext = strlen($url) > 35 ? substr($url, 0, 21).'...'.substr($url, -10) : $url;
			$text = preg_replace('~^'.$url.'~m',"<a href=\\"http://$url\\" target=\\"_blank\\" rel=\\"nofollow\\">$urltext</a>",$text);
		}
	}
	$text = str_replace('qmark','?',$text);
	return $text;
}

@ulthane,

Congratulations - treat yourself to a Special Mocha Coffee with Double Cream :slight_smile:

Here is my lengthy, revised, more readable version:



function url_maker($text)
{
  $result = array();

  # KLUDGE to  replace and finally restore line-feeds - // ordinary-space ALT 400 ordinary-space
  $x400	= " É ";

  $text = str_replace('http://', '', $text);
  $text = str_replace( '<br />', $x400, $text);

  $items    = explode(' ', $text);

  foreach( $items as $item ):

      if( strpos($item, '.') && ( ! strpos($item, '."') ) )
      {
          $urltext  = strlen($item) > 99 ? substr($item,0,17) .'...' : $item;
          $result[] = '<a href="http://'
       		.   $item
           		.   '" target="_blank" rel="nofollow">'
           		.   $urltext
           		. '</a>';
      }
      else
      {
        $result[] =  $item;
      }

  endforeach;

  #DEBUG
    echo '<pre>';
      #print_r($result);
    echo '</pre>';

  # Beware: $result array is changed to string
  $result = implode($result, ' ' );
  $result	= str_replace( $x400,'<br />', $result);

  return $result;
}