TheEvilMonkeyMan Posted November 30, 2010 Share Posted November 30, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/ Share on other sites More sharing options...
sasa Posted November 30, 2010 Share Posted November 30, 2010 try '/(ht|f)tps?:\/\/[A-Za-z0-9_.-]{3,}\/?\s|$/' not tested Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141326 Share on other sites More sharing options...
JAY6390 Posted November 30, 2010 Share Posted November 30, 2010 try \b instead of \s at the end Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141328 Share on other sites More sharing options...
TheEvilMonkeyMan Posted November 30, 2010 Author Share Posted November 30, 2010 The \b made it cut the URL off after the .com leaving out the /file.htm etc and not too much luck with the \s|$ - not sure why. And there's still a strange thing happening where a random <a will show up, breaking my website layout. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141332 Share on other sites More sharing options...
JAY6390 Posted November 30, 2010 Share Posted November 30, 2010 can you post some sample data Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141334 Share on other sites More sharing options...
TheEvilMonkeyMan Posted November 30, 2010 Author Share Posted November 30, 2010 http://www.ultimateglobes.com/Illuminated-World-Globes-s/4.htm you should find some nice ones there You can find a variety of styles of gemstone world globes here: http://www.onlyglobes.com/ http://www.1worldglobes.com/1WorldGlobes/GemstoneGlobes/gemstoneglobes.htm http://www.worldglobes.com/ Or Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141335 Share on other sites More sharing options...
JAY6390 Posted November 30, 2010 Share Posted November 30, 2010 Try this $string = preg_replace_callback('/(ht|f)tps?:\/\/\S+/', 'trim_urls_callback', $string); Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141336 Share on other sites More sharing options...
TheEvilMonkeyMan Posted November 30, 2010 Author Share Posted November 30, 2010 Jay, you da man. That seems to be working for all my links. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141338 Share on other sites More sharing options...
JAY6390 Posted November 30, 2010 Share Posted November 30, 2010 no problem Quote Link to comment https://forums.phpfreaks.com/topic/220231-url-trimming-function/#findComment-1141339 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.