FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to calculate the amount of characters to remove?





Diablosblizz
Hey, I have just realized that I am going to have to alter my code to remove more or less characters to remove so the URL can fit on a certain part of my site. Right now, I have it set to remove 5 characters but I noticed that the more characters you have the larger the URL will be.

So, is there anyway to calculate how many characters there are and to remove enough characters to lower it down to 29 characters? I have tried calculating the amount of characters in the string and removing 29 and that just makes it smaller! I am not good with math but it doesn't seem to work.

What I have now:

Code:
if(strlen($url) >= 29) {
$amount = strlen($url) - 29;
$newstring1 = substr($url, 0, $amount);
$newstring = "$newstring1...";
} else if(strlen($string) <= 28) {
$newstring = "$url";
}


Does anybody know anything I could do? Thanks!
mathiaus
Some questions before I look properly..

1) Do you want it to be 29 chars in length including the dots at the end?
2) Do want the end cut off, or a portion in the middle?
3) What is $string (from the elseif) and where does it come from?
kv
let us say your site accommodates only $x characters ($x could be 29 or 50 or whatever, depending on the area of your website.

Then

Code:


$len=strlen($url);
if($len>$x)  //<-- check if url is more than $x characters
     $url=substr($url, 0, $x);  //<-- if yes, truncate it to $x characters

Diablosblizz
Thanks kv, that worked perfectly!
Related topics
Boot up Windows XP 60 times faster
Fortran Tutorials(77)
10 Reasons why PC's crash
Java tutorials
Shadow of The Colossus - video game
Master Tutorial on SEO
Evolution, the religion
All About WoW
X Men 4?
Gravity and Acceleration
I believe characters are Japanese, but not sure
Reguly forum - tlumaczenie - radze przeczytac
Outrage over Virginia Tech game
OFFICIAL : First Ever Frihost Writing Contest !
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.