|
-
Oct 11th, 2006, 07:33 AM
#1
Thread Starter
Frenzied Member
Reducing string length
Hi there, now i have a string that i echo into html.
I need to cut this string down if it exceeds the width of the table it resides in.
I have looked into cutting it down according to how many characters it has, however as im using Tahoma, there is a big difference between 'w' and 'i'.
Anyone have any ideas on how this can be done?
-
Oct 11th, 2006, 07:57 AM
#2
Re: Reducing string length
Because of the way a browser can handle fonts (the USER has the ultimate decision on how big your font will display and can always override your font settings), I don't suggest that you try to do this on a pixel-by-pixel scale. It can be rather messy, especially when you're using a font that doesn't have same-sized letters like Courier or other native system/command-line-like fonts. The most I can tell you is to check how many W's fit in your table (the W is the widest letter, so it will be the best for you), and make the maximum number that, and cut off anything longer and then append some periods. This would work for that:
PHP Code:
<?php
$max = 6; //maximum number of characters
$str = "blahblahblahblah";
if(strlen($str) > $max)
$dispstr = substr($str, 0, $max - 1) . "..";
else
$dispstr = $str;
echo $dispstr;
?>
Sorry, though, that might not be completely what you were looking for, but it's about the only easy way.
Alternatively, you could always go into an image editor and type out the entire alphabet in Tahoma, figure out each width in pixels and then store that value in an array, and then pick apart your string letter by letter to determine what the overall width is, and at what letter you can cut it off at. Like I said before, I don't suggest it, but you could always do it. It would be a lot slower, too.
Edit: oh, and if you ever changed your font-face, you would have to figure out the new widths for each letter all over. that would be fun.
Last edited by kows; Oct 11th, 2006 at 08:00 AM.
-
Oct 11th, 2006, 09:13 AM
#3
Thread Starter
Frenzied Member
Re: Reducing string length
Thanks, well your seond option of measure the font, dare i say it seems really appealing, thanks for bringing the fact that users can infact change their font size.
I will need to review this a little more thanks
-
Oct 23rd, 2006, 05:13 AM
#4
Addicted Member
Re: Reducing string length
aren't there any space " " in string ?
-
Oct 23rd, 2006, 05:52 AM
#5
Re: Reducing string length
Code:
td.limited { overflow: hidden; text-overflow-mode: ellipsis; }
Note that that probably won't work in any current CSS implementation, so it's practically useless at this point in time.
-
Oct 23rd, 2006, 06:39 AM
#6
Thread Starter
Frenzied Member
Re: Reducing string length
That looks interesting, pen will that not work, with my current ttables?
I will; take a look later, not at my dev machine now...
-
Oct 23rd, 2006, 06:41 AM
#7
Re: Reducing string length
No, it won't work on anything, and probably won't for a few years; but it's vaguely amusing to know about.
-
Oct 23rd, 2006, 06:58 AM
#8
Re: Reducing string length
 Originally Posted by kows
Edit: oh, and if you ever changed your font-face, you would have to figure out the new widths for each letter all over. that would be fun.
And if the user doesn't have a Tahoma font (I don't on the Linux system), he'll still see it wrong.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|