Automatically shorten long hyperlinks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Automatically shorten long hyperlinks

Post 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
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Automatically shorten long hyperlinks

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Automatically shorten long hyperlinks

Post 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.
Post Reply