willdanceforfun
willdanceforfun

Reputation: 11240

Extracting the text from a link

I have a small function which goes through a bunch of text and looks for any urls in text form and converts them into html a's:

e.g

normal text lipsum etc http://www.somewebsitelink.com lipsum lipsum

becomes:

normal text lipsum etc <a href="www.somewebsitelink.com">http://www.somewebsitelink.com</a> lipsum lipsum

My function is as follows:

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

  $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a target="_blank" href="http://\\2">\\2</a>', $text);

  return $text;
}

This is all works ok, but where I use this function and print out the html is in a limited width space and sometimes links end up much bigger then can fit in the space and I end up with overflow.

I'm wondering how I might go about doing 2 things:

a. remove the unecessary from the the text ie 'http://' so I would end up with

<a href="http://www.somewebsitelink.com">www.somewebsitelink.com</a>

and

b. If the text is greater than 20 characters, cut out everything after it and put in a few dots. e.g:

<a href="http://www.somewebsitelink.com">www.somewebsitelin...</a>

I'm wondering if I might have to do this without using regular expressions, but then again my understanding of reg exp is fairly limited.

Upvotes: 1

Views: 135

Answers (3)

Yellowledbet
Yellowledbet

Reputation: 147

I think this will do what you need. It takes a url as its only paramater and removes the leading 'http://www." then returns the string if it is less than 20 characters. if it is more than 20 characters it returns the first 17 characters and appends '...'.

function get_formatted_url($url){
    $url = trim($url);
    $url = str_replace("http://www.", "", strtolower($url)); 
    if(strlen($url) > 20){
        return substr($url, 0, 16) . '...';
    }else{
        return $url;
    }
}

Edit: An example using preg_replace_callback()

function get_formatted_url($url){
    $url = $url[0];
    $formatted_url = trim($url);
    $formatted_url = str_replace("http://www.", "", strtolower($formatted_url)); 
    if(strlen($formatted_url) > 20){
        return '<a href="'.$url.'" />'. substr($formatted_url, 0, 16) . '... </a> ';
    }else{
        return '<a href="'.$url.'" />'. $formatted_url . '</a> ';
    }
}

function linkify($text){
    $reg = '(((f|ht)tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)';
    $text = preg_replace_callback($reg, "get_formatted_url", $text);
    return $text;
}



$text = "abcd http://www.abc.com?hg=alkdjfa;lkdjfa;lkdjfa;lkdsjfa;ldks abcdefg http://www.abc.com";
echo linkify($text);

Upvotes: 2

nkamm
nkamm

Reputation: 627

$link = 'http://www.somewebsitelink.com';
function linkify($text, $maxLen = 15)
{
  return preg_replace_callback('(((f|ht){1}tp://)([-a-zA-Z0-9@:%_\+.~#?&//=]+))', function($t) use ($maxLen) {
      if ( strlen($t[3]) > $maxLen)
        $t[3] = substr_replace($t[3], '...', $maxLen);

      return sprintf('<a target="_blank" href="%s">%s</a>', $t[0], $t[3]);
  }, $text);
}

header('content-type: text/plain');
echo linkify($link);

Code for PHP <= 5.2

$link = 'http://www.somewebsitelink.com';
function linkify($text, $maxLen = 15)
{
    $funcBody = <<<FUNC
if ( strlen(\$t[3]) > \$maxLen)
  \$t[3] = substr_replace(\$t[3], '...', \$maxLen);   

return sprintf('<a target="_blank" href="%s">%s</a>', \$t[0], \$t[3]);   
FUNC;
      $func = create_function(
        '$t, $maxLen =' . $maxLen,
        $funcBody
    );
    return preg_replace_callback('(((f|ht){1}tp://)([-a-zA-Z0-9@:%_\+.~#?&//=]+))', $func, $text);
}

header('content-type: text/plain');
echo linkify($link);

Results in

<a target="_blank" href="http://www.somewebsitelink.com">www.somewebsite...</a>

Upvotes: 2

ujjwalwahi
ujjwalwahi

Reputation: 342

Use php substr function. For example:

<?php substr($text,0,10);?>

Upvotes: 0

Related Questions