Jump to content

Word Wrap function problem


peaforabrain

Recommended Posts

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?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/49705-word-wrap-function-problem/
Share on other sites

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); 
} 

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.