Stephen68 Posted February 25, 2009 Share Posted February 25, 2009 I'm not sure why this is not working so I thought I would post it here. I'm just looping through information that I am getting from a database with a while loop and displaying it in HTML. The problem that I found out is that some of the text has no breaks in it and so it would stretch the cells out pass what I would like. It was suggested to me that I use wordwrap to force the text to wrap as I would like it. $title = wordwrap($row['Job_Title'], 10, "<br />\n"); This is what I came up with, I assume that it would count over 10 chars and put in a break. The thing is it's not doing that, is there something wrong with my syntax of it? Thanks for looking at this for me Stephen Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/ Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 I think the better solution is to set the width of your cells so they cannot be broken. <table width="500"> <tr> <td width="100">info</td> <td width="100">info</td> <td width="100">info</td> <td width="100">info</td> <td width="100">info</td> </tr> </table> This way, the text can never break 100px in size for each cell... Hope that helps! Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771050 Share on other sites More sharing options...
Stephen68 Posted February 25, 2009 Author Share Posted February 25, 2009 Ya I thought about doing that but I had the table width set and it still pushed the cell out. I still don't see why the wordwrap is not working though ;( Stephen Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771064 Share on other sites More sharing options...
blintas Posted February 25, 2009 Share Posted February 25, 2009 Try this <?php function one_wordwrap($string,$width){ $s=explode(" ", $string); foreach ($s as $k=>$v) { $cnt=strlen($v); if($cnt>$width) $v=wordwrap($v, $width, "<br />", true); $new_string.="$v "; } return $new_string; } ?> Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771089 Share on other sites More sharing options...
Stephen68 Posted February 25, 2009 Author Share Posted February 25, 2009 Worked great thank you for your time in this, still wonder why the wordwrap directly in the display code would not work ;( Thanks again Stephen Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771095 Share on other sites More sharing options...
JonnoTheDev Posted February 25, 2009 Share Posted February 25, 2009 Im sure word wrapping can also be done using CSS Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771107 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 What type of data is held in the job_title, is it just one word? Or does it have a "\n" (line break) or any other weird items like that? wordwrap can be picky. And that could be the issue. Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771121 Share on other sites More sharing options...
Stephen68 Posted February 25, 2009 Author Share Posted February 25, 2009 It could be a few words and would most likely contain /n in it. Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771174 Share on other sites More sharing options...
premiso Posted February 25, 2009 Share Posted February 25, 2009 It could be a few words and would most likely contain /n in it. The \n is where the issue arises, unfortunately. But as long as the other method works you are good. I just figured it would be good for you to have an explanation why it was not working properly. The \n to wordwrap is already separating the string. Alternatively, if \n is meant to do that you might want to look into nl2br as that may be an answer. Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771178 Share on other sites More sharing options...
Stephen68 Posted February 25, 2009 Author Share Posted February 25, 2009 Thanks for taking the time to explain this to me, I believe that I could make that work and will try it out. Again thanks for you time Stephen Link to comment https://forums.phpfreaks.com/topic/146869-solved-wordwrap-help/#findComment-771187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.