Posted by: prajapatinilesh | August 8, 2007

PHP – Make URLs clickable (And short down)

<?php

$text = ‘Some text here with an URL here http://www.superlongurl.com/superlongfoldername/filename.ext and some more text here blah blah blah.’;

/*
Makes the URLs clickable and shorts them down to 35 chars max since it’s the default value and no other one was specified.
*/

echo parse_urls($text);

/*
Outputs: Some text here with an URL here http://www.superlongurl.c…ilename.ext and some more text here blah blah blah.
*/

echo parse_urls($text, 40);

/*
Makes the URLs clickable, but doesn’t short them down.
*/

echo parse_urls($text, false);

/*
Makes the URLs clickable, and sets the target attribute to _blank, so they will open in a new window. This can be left in blank. The default target is _self.
*/

echo parse_urls($text, false, ‘_blank’);

function parse_urls($text, $maxurl_len = 35, $target = ‘_self’)
{
if (preg_match_all(‘/((ht|f)tps?:\/\/([\w\.]+\.)?[\w-]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)\’<>\,\!]+)/si’, $text, $urls))
{
$offset1 = ceil(0.65 * $maxurl_len)2;
$offset2 = ceil(0.30 * $maxurl_len)1;

foreach (array_unique($urls[1]) AS $url)
{
if ($maxurl_len AND strlen($url) > $maxurl_len)
{
$urltext = substr($url, 0, $offset1) . ‘…’ . substr($url, –$offset2);
}
else
{
$urltext = $url;
}

$text = str_replace($url, ‘<a href=”‘. $url .‘” target=”‘. $target .‘” title=”‘. $url .‘”>’. $urltext .‘</a>’, $text);
}
}

return $text;
}
?>

Advertisements

Responses

  1. Good day!,

  2. Dude i was having prob implementing your code this contains bugs

  3. this code is working()

    function parse_urls($text, $maxurl_len = 35, $target = ‘_self’)
    {

    if(preg_match_all(‘/((ht|f)tps?:\/\/([\w\.]+\.)?[\w-]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)”\’\,\!]+)/si’, $text, $urls))
    {

    $offset1 = ceil(0.65 * $maxurl_len) – 2;
    $offset2 = ceil(0.30 * $maxurl_len) – 1;

    foreach (array_unique($urls[1]) AS $url)
    {
    if (strlen($url) > $maxurl_len)
    {
    $urltext = substr($url, 0, $offset1) . ‘…’ . substr($url, -$offset2);
    }
    else
    {
    $urltext = $url;
    }

    $text = str_replace($url, ‘‘. $urltext .’‘, $text);
    }
    }

    return $text;
    }

  4. also change the quotes when implementing

  5. which quotes i am getting a parse error on line 6

  6. why cant you just do this
    $text = “Some text here with an URL here and some more text here blah blah blah.”;

  7. its deleting the html tags in the ones above

  8. This doesn’t work if you have “http://www.test.com/11 and http://www.test.com/1” since the $urls are already saved from the preg_match_all().

    When executing str_replace() for http://www.test.com/1 it finds the inserted href from http://www.test.com/11.

    Probably need to filter $text each time.

  9. what do i have to change? Could you please upload the code somewhere in a codebox or at gist.github.com?

  10. Found some bits in the net and put this together (only wont link URL-s beginning with www only with http).
    So a alternate way to go at this, ps u can get a array with all the different or an array with all url’s from the function too.

    function lingista_tekst( $stringa , $target = ‘_blank’){
    $m = preg_match_all(‘/((ht|f)tps?:\/\/([\w\.]+\.)?[\w-]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\)”\’\,\!]+)/si’, $stringa, $match);
    if ( $m ) {
    $links= array_values( array_unique( $match[0] ));
    for ( $j=0; $j<( count( $links )); $j++ ) {
    $stringa = str_replace( $links[$j] , ('‘. $links[$j] .’‘ ), $stringa);
    }
    }
    return $stringa;
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: