Page 1 of 1

Shortening a URL?

Posted: Wed Feb 23, 2005 11:32 pm
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

Posted: Wed Feb 23, 2005 11:52 pm
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..

Posted: Wed Feb 23, 2005 11:54 pm
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]

Posted: Thu Feb 24, 2005 1:54 am
by Mr Tech
Awesome. Cheers mate :)