svgmx5 Posted July 1, 2012 Share Posted July 1, 2012 my issue is the following: I'm retrieving data from my database and putting the result data in an array. I then implode the array so each row of data has a "," at the end of it. The issue is that when i output this data it does not break as it should after it reaches the end of my block instead if keeps on going overlapping other blocks of data on my site. What i want to do and can't figure out is how can i split the data in rows based on a certain number of characters, but at the same time make sure that a word is not broken. So for example an string like : red, white, blue, pink, orange, black. I want this string to add a "<br/>" after it reaches a certain number of characters but make sure that a word is not broken. so i can have something like : red, white, blue, pink orange, black Instead of this: red, white blue, pink, ora nge, black I have tried using the CSS attribute of "word-wrap: break-word" but this just breaks the word as the above example. Anyways, i have placed my code below, hope someone can help me out. Thanks in advanced $array = array(); $i = 0; if($results!=0){ while($row = mysql_fetch_assoc($query)){ $array[] = $row['name'].' '; $i++; } echo implode(',',$array); } Quote Link to comment https://forums.phpfreaks.com/topic/265066-word-wrap-a-string-in-an-array/ Share on other sites More sharing options...
requinix Posted July 1, 2012 Share Posted July 1, 2012 Implode on a ", " (comma + space) instead. Or if you really don't want that, try a comma + a zero-width space. Quote Link to comment https://forums.phpfreaks.com/topic/265066-word-wrap-a-string-in-an-array/#findComment-1358302 Share on other sites More sharing options...
svgmx5 Posted July 1, 2012 Author Share Posted July 1, 2012 can't believe it was this simple to do this. Just another question, while this is working fine, i was wondering if there was a way to remove the ',' at the end of the word after it makes the break. Right now the string shows up like: red, black, white, pink, orange Its not a huge deal but if possible i'd like to be able to remove the last ',' at the end of each line break. Quote Link to comment https://forums.phpfreaks.com/topic/265066-word-wrap-a-string-in-an-array/#findComment-1358327 Share on other sites More sharing options...
requinix Posted July 1, 2012 Share Posted July 1, 2012 But... it's a list. You don't just remove commas because the list spans multiple lines. There isn't really any good way to do it. You'd have to know in your PHP where the line break is going to be but that depends on fonts and sizes and widths and all manners of things which you can't really know for sure about. Quote Link to comment https://forums.phpfreaks.com/topic/265066-word-wrap-a-string-in-an-array/#findComment-1358333 Share on other sites More sharing options...
svgmx5 Posted July 1, 2012 Author Share Posted July 1, 2012 That's what i figured, but thought it be worth a shot asking. Anyways, thank you for your help i guess this has been resolved! Quote Link to comment https://forums.phpfreaks.com/topic/265066-word-wrap-a-string-in-an-array/#findComment-1358419 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.