Jump to content

URL trimming function


TheEvilMonkeyMan

Recommended Posts

Hi,

I have a function using preg_replace_callback to go through a string replacing all instances of URLs with a link to that URL. It works, but not when the link is at the end of the string, since it uses white space to mark the end of the URL. I'm sure it can't be that hard. Could somebody help me out?

 

function trim_urls_callback($matches)
{
$temp = substr($matches[0], 0, 15);
$final = "<a href=\"$matches[0]\" title=\"$matches[0]\">$temp…</a> ";
return $final;
}

function trim_urls($string)
{
$string = preg_replace_callback('/(ht|f)tps?:\/\/[A-Za-z0-9_.-]{3,}\/?\s/', 'trim_urls_callback', $string);
return $string;
}

Link to comment
https://forums.phpfreaks.com/topic/220231-url-trimming-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.