Shortening a URL?

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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Shortening a URL?

Post by Mr Tech »

I did a search and couldn't find anything much...

Here is my code:

Code: Select all

$urlsmall = substr($url, 0, "55")."...";
Now what that does is cut the URL off after 55 chars and puts a ... afterwards to show that its is longer...

However, if the URl has less then 55 chars its still puts the ... after it... is there anyway I can stop it from adding the ... if it is less then 55 chars?

What would eb even better would be to set it up like vbulletin does their links. E.g:

http://www.domain.com/folde...th/filename.php

Any ideas on how to do that?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strlen()

one way of doing vBulletin's style is just substr the beginning and ending, slapping the ellipsis between (when the string is above x characters)

other ways are using regular expressions to only chop certain bits..
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

you can try with:

Code: Select all

if (strlen($url) > 55)  //strlen function returns the length of the string
    $urlsmall = substr($url, 0, "55")."...";
else
     $urlsmall = $url;

feyd | please use

Code: Select all

tags while the

Code: Select all

tags are offline.[/color]
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Awesome. Cheers mate :)
Post Reply