PHP smart linking

I have Private Message system I made on my website, but when users posts link to others it shows them unclickable long URLs.
How I can make this links appear like that :… Instead of (Oh, it has just shortened my link like I want :D) and make it clickable?


Can you clarify please - do you want to make the links themselves shorter, or do you just want the display to be shorter? As you know, the clickable display part doesn’t need to have the complete link in it (or in fact any of the link), so you could just write a function to take in the entire link, drop the centre part of it out and add some ellipsis, then return that to be the display portion, but with the effect that the user would have to rely on using the ‘copy url’ on their brower if they wanted to paste the link elsewhere.

You’ll need a good regex pattern that can recognize links and turn them into clickable URLs. I can’t vouch for how well these work, but as a starting point take a look at pages like this, or have a search on Google.

Once the regex functionality is implemented the hard work is over. You can use preg_replace_callback to loop over every link, check if the link is too long with strlen, and shorten it with substr.