elmas156 Posted September 1, 2010 Share Posted September 1, 2010 This sounds like it should be pretty simple but I've looked for a while and I'm having trouble finding an answer. What I have is a simple table with 3 columns with predetermined widths. When the text that is queried from the database is inserted into the table, and the text string is longer than the column width, it pushes the text to a second line, which throws the whole table out of whack. Instead, I want the text to be cut off before going to the next line. For example: This is what is happening: This is a sample string of text. This is what I'm trying to do: This is a sample string... Can anyone tell me where to find the solution to this problem? Not that it's needed, but here is some simple code that I'm working with: <?php $message = "This is a sample string of text."; echo "<table width=\"150\" height=\"20\"border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"; echo "<tr>"; echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">Column 1</td>"; echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">Column 2</td>"; echo "<td width=\"50\" height=\"20\" align=\"left" valign=\"top\">$message</td>"; echo "</tr>"; echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 1, 2010 Share Posted September 1, 2010 I've had this problem too. What I do is put this around the text: <div style="overflow:auto; width:100px;">text goes here</div>. You have to set the width to the same width as your column though. The problem is, this doesn't add the "..." that you want. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2010 Share Posted September 1, 2010 Or you can truncate the string with substr() and append the . . . Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 1, 2010 Author Share Posted September 1, 2010 Pikachu, can you tell me more about your solution? It sounds like it would be worth a try but I'm not sure how I would use substr() to truncate if I don't know how long the string is before hand. Is there a way to truncate by determining how many characters you want displayed rather than how many characters you DON'T want displayed? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2010 Share Posted September 1, 2010 Posting from my phone, so cutting/pasting a link is difficult, but if you google substr, the first result should be for the function in the php manual. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 1, 2010 Author Share Posted September 1, 2010 I found it... but I edited my last message to this: I'm not sure how I would use substr() to truncate if I don't know how long the string is before hand. Is there a way to truncate by determining how many characters you want displayed rather than how many characters you DON'T want displayed? By the way thanks for your help! Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 1, 2010 Share Posted September 1, 2010 Since you've created this topic, I'm really interested in adding this to my site as well. I found this code online. It appears to be exactly what we're looking for, but it needs to be edited some: <?php $file = "Hellothisfilehasmorethan30charactersandthisfayl.exe"; function funclongwords($file) { if (strlen($file) > 30) { $vartypesf = strrchr($file,"."); $vartypesf_len = strlen($vartypesf); $word_l_w = substr($file,0,15); $word_r_w = substr($file,-15); $word_r_a = substr($word_r_w,0,-$vartypesf_len); return $word_l_w."...".$word_r_a.$vartypesf; } else return $file; } ?> One problem with this code is that it puts the "..." in the middle of the words, which is good if you want to see the beginning and end of the line, but I want the "..." at the end of the words. Will you do me a favor and give me your edited version if it works for you? I'm not that good at PHP Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 1, 2010 Share Posted September 1, 2010 Never mind, I was able to edit it. I made it short and simple: <?php if (strlen($words) > 10) { $short = substr($words,0,10); echo ''.$short.'...'; // This will display 10 letters and ... (Textistool...) } else { echo 'The text that isn't too long goes here'; } ?> As you can see, it's easy to change how long you want the text to be before the ... I did notice one problem with using this code though. If one of your columns has 10 W's (WWWWWWWWWW), it's much longer than 10 a's (aaaaaaaaaa), so it still may go over to the next line . It's not a huge problem I guess. To fix this problem, I use this before the text: <div style="overflow:auto; width:100px;">TEXT HERE</div> Make it the width of your column, and those W's will just be cut off instead of go to the next line, but you won't be able to see the "..." because it will be cut off too.. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 1, 2010 Author Share Posted September 1, 2010 Thanks Ethan, I'll give it a try and let you know how mine turns out. Quote Link to comment Share on other sites More sharing options...
elmas156 Posted September 1, 2010 Author Share Posted September 1, 2010 OK Ethan, here is the solution to our problem. It works exactly how we want it, with the "..." at the end of the string. Plus it's a lot simpler than the other code. <?php $text = "This is whatever text you want to go into the table."; if (strlen($text) > 40) { $short_text = substr($text,0,40); echo "$short_text..."; } else { echo "$text"; } ?> Thanks for your help in figuring this one out. Quote Link to comment Share on other sites More sharing options...
ethan89 Posted September 1, 2010 Share Posted September 1, 2010 No prob. I'm glad you made this topic, or I would have never gotten it done myself. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.