peaforabrain Posted May 2, 2007 Share Posted May 2, 2007 I've been stuck on a problem to limit text in a css cell. I would like it to be 29 characters wide and 3 lines high only (this last bit is the problem). I have tried many of the examples on the php.net site, and none do the trick. I have also tried modifying them. I wondered whether it is possible to acheive this? Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/ Share on other sites More sharing options...
kenrbnsn Posted May 2, 2007 Share Posted May 2, 2007 Please post the code you've tried between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243713 Share on other sites More sharing options...
peaforabrain Posted May 2, 2007 Author Share Posted May 2, 2007 function justify($text, $width, $break) { $marker = "__$%@random#$()__"; // lines is an array of lines containing the word-wrapped text $wrapped = wordwrap($text, $width, $marker); $lines = explode($marker, $wrapped); $result = ""; foreach ($lines as $line_index=>$line) { $line = trim($line); $words = explode(" ", $line); $words = array_map("trim", $words); $wordcount = count($words); //$wordlength = strlen(implode("/([^\s]{6})/","$1 ",$words) $wordlength = strlen(implode("", $words)); // if (3*$wordlength < 2*$width) { // don't touch lines shorter than 2/3 * width // continue; // } $spaces = $width - $wordlength; $index = 0; do { $words[$index] = $words[$index] . " "; $index = ($index + 1) % ($wordcount - 1); $spaces--; } while ($spaces>0); $lines[$line_index] = implode("", $words); } return implode($break, $lines); } Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243724 Share on other sites More sharing options...
Daniel0 Posted May 2, 2007 Share Posted May 2, 2007 I would like it to be 29 characters wide and 3 lines high only (this last bit is the problem). I don't get why you'd want to do that. That would limit you to 87 characters only (or 89 if you count linefeeds at the end). Anyways, the foreach loop will never get to the end each time since you've commented out the if statement around the continue structure. I.e. this: $spaces = $width - $wordlength; $index = 0; do { $words[$index] = $words[$index] . " "; $index = ($index + 1) % ($wordcount - 1); $spaces--; } while ($spaces>0); $lines[$line_index] = implode("", $words); will never get executed. Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243731 Share on other sites More sharing options...
effigy Posted May 2, 2007 Share Posted May 2, 2007 If you're measuring by characters and lines you'll need to use a fixed-width font: <html> <head> <style type="text/css"> div { font-family: courier new; font-size: 10pt; line-height: 12pt; width: 17.4em; height: 34pt; border: 1pt solid black; } </style> </head> <body> <div> ABCDEFGHIJKLMNOPQRSTUVWXYZabc ABCDEFGHIJKLMNOPQRSTUVWXYZABC ABCDEFGHIJKLMNOPQRSTUVWXYZABC </div> </body> </html> Height = font-size + (line-height * (lines - 1)). Width = (font-width * characters) / font-size. Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243737 Share on other sites More sharing options...
peaforabrain Posted May 2, 2007 Author Share Posted May 2, 2007 Anyways, the foreach loop will never get to the end each time since you've commented out the if statement around the continue structure. I.e. this: Sorry about that, I was just editing it afetr trying something else, and just pasted it in, without checking what i'd done. Anyway thanks for the help. I will try that function, but I can't see it working. Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243747 Share on other sites More sharing options...
peaforabrain Posted May 2, 2007 Author Share Posted May 2, 2007 This may seem a bit thick, but how do I reference this function; say($chars = array()) I have never come accross that before. Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243754 Share on other sites More sharing options...
Daniel0 Posted May 2, 2007 Share Posted May 2, 2007 This may seem a bit thick, but how do I reference this function; say($chars = array()) I have never come accross that before. Heh... it was from my signature. The code in my signature displays "hello world". Quote Link to comment https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/#findComment-243773 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.