firestarter30 Posted August 2, 2010 Share Posted August 2, 2010 Hello all 1st post here I red in an older post how to add <br /> using the wordwrap() function , ok i used it it worked , but not as i was expecting and here is the point i need your help. I have a table which i pull some strings from the database.Horizontal space is limited to i wanted arround 70 charecters per line. The problem i have is because i used the above function the text ofcourse always keeps 70 characters and makes the text formation not to look so appealing..here is what i mean as an example Pros: bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla This is how the output is looks like in the page. I would like something like this : Pros: bla bla bla bla bla bla bla bla bla bla bla blabla blabla bla bla bla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla bla bla blabla blabla blabla blabla blabla blabla blabla blabla blabla blabla bla If there is a way to tell php : hey make the 1st line to have 70 characters and all the rest to have 90. Is a visual problem in other words. The code is this Pros: <?php echo wordwrap(($row_site_list['pros']),70,"<br />",true)?><br /> Any ideas, pointers? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/ Share on other sites More sharing options...
Pikachu2000 Posted August 2, 2010 Share Posted August 2, 2010 I'm thinking the easiest thing would be to concatenate 'Pros: ' to the rest of the string. $string = 'Pros: '; string .= $row_site_list['pros']; echo wordwrap($string, 70, '<br />', true) Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/#findComment-1094340 Share on other sites More sharing options...
firestarter30 Posted August 3, 2010 Author Share Posted August 3, 2010 I'm thinking the easiest thing would be to concatenate 'Pros: ' to the rest of the string. $string = 'Pros: '; string .= $row_site_list['pros']; echo wordwrap($string, 70, '<br />', true) Couldnt agree with you more Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/#findComment-1094389 Share on other sites More sharing options...
firestarter30 Posted August 3, 2010 Author Share Posted August 3, 2010 Hmmm i think it was from the beginning my mistake cause after i insert to the css a padding 5px left, and 5px right, the problem solved without any wordwrap php One other thing i would like to ask. Is there a way after a certain character length, lets say 100 , to output " ...more " so i can link "...more " to a details page? Thanks again for your time Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/#findComment-1094410 Share on other sites More sharing options...
monkeytooth Posted August 3, 2010 Share Posted August 3, 2010 Don't feel like retyping it.. But heres a short little concept I use frequently in some of my projects.. Can be easily edited to your needs as well.. http://monkeytooth.net/2010/06/php-shorten-strin/ Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/#findComment-1094411 Share on other sites More sharing options...
Psycho Posted August 3, 2010 Share Posted August 3, 2010 Here's a simple function that will truncate a string if it is over the specified number of characters (and add ellipses, or whatever characters you wish): function truncateString($string, $length, $ellipse='...') { if (strlen($string) <= $length) { return $string; } return array_shift(explode("\n", wordwrap($string, $length))) . $ellipse; } $text = "This is a long string with many, many words and it should be truncated"; echo truncateString($text, 45); //Output: "This is a long string with many, many words..." Quote Link to comment https://forums.phpfreaks.com/topic/209611-wrap-word-lenth-problem/#findComment-1094422 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.