SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    4 Post(s)
    0 Thread(s)

    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 ) and make it clickable?

    Owner of uReviews & MovieDB - Movie Database

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Nantwich, Cheshire
    2 Post(s)
    0 Thread(s)
    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. - my homage to a car from the 1970s

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    0 Post(s)
    0 Thread(s)
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts