Page 1 of 1

Automatically shorten long hyperlinks

Posted: Tue Apr 29, 2008 8:33 pm
by EoN
I've been googling for what I thought would be an easy thing to find.

I'm after a script/function/code to parse a body of text and automatically shorten any long hyperlinks within it.

In a new site of mine, a couple of users have posted URL's which are quite long and push out the size of the fixed width column breaking it.

So basically I'm want a shortenUrl($content) sort of function to shorten any URL longer than $length characters, as follows:
This is a body of text here is a hyperlink http://www.aaa.com/bbb/ccc.php?ddd=fff yes goodbye
to be passed through the function and return something like...
This is a body of text here is a hyperlink <a href="http://www.aaa.com/bbb/ccc.php?ddd=fff" ... /bbb...</a> yes goodbye
Anyone used, or know of something like this?

Cheers,
EoN

Re: Automatically shorten long hyperlinks

Posted: Tue Apr 29, 2008 10:30 pm
by nowaydown1
I don't know of anything pre-built, but you could use a regular expression to find your hyperlinks, build a shortened version based on your matches, then replace the matches with your newly built shortened version. You might try 'autolink php' in google or something. That should get you started on the regex end.

Re: Automatically shorten long hyperlinks

Posted: Wed Apr 30, 2008 11:08 am
by Kieran Huggins
there's one in the phpbb3 source somewhere, and I like the way it shortens in the middle of the url, since the end can be an important distinction.

It's almost certainly a regex - look through the code for "preg_replace" calls.