note 81236 added to function.wordwrap

From: Date: Tue, 19 Feb 2008 20:21:34 +0000
Subject: note 81236 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-136283@lists.php.net to get a copy of this message
I wrote a justification function for a project of mine. It uses the wordwrap function and provides four justification options: * Left; typically, the leftmost words receive the most padding * Right; vice versa; the rightmost words receive the most padding * Both; tries to evenly distribute the padding among leftmost and rightmost words * Average; most complicated, uses an average of the three previous algorithms. I'd say this one produces the best result as it's more distributed in the center. It does not justify the last line. <?php define('JPAD_LEFT', 1); // More spaces are added on the left of the line define('JPAD_RIGHT', 2); // More spaces are added on the right of the line define('JPAD_BOTH', 4); // Tries to evenly distribute the padding define('JPAD_AVERAGE', 8); // Tries to position based on a mix of the three algorithms function justify($input, $width, $mode = JPAD_AVERAGE) { // We want to have n characters wide of text per line. // Use PHP's wordwrap feature to give us a rough estimate. $justified = wordwrap($input, $width, "\n", false); $justified = explode("\n", $justified); // Check each line is the required width. If not, pad // it with spaces between words. foreach($justified as $line) { if(strlen($line) != $width) { // Split by word, then glue together $words = explode(' ', $line); $diff = $width - strlen($line); while($diff > 0) { // Process the word at this diff if ($mode == JPAD_BOTH) $words[$diff / count($words)] .= ' '; else if($mode == JPAD_AVERAGE) $words[(($diff / count($words)) + ($diff % count($words)) + (count($words) - ($diff % count($words)))) / 3] .= ' '; else if($mode == JPAD_LEFT) $words[$diff % count($words)] .= ' '; else if($mode == JPAD_RIGHT) $words[count($words) - ($diff % count($words))] .= ' '; // Next diff, please... $diff--; } } else { $words = explode(' ', $line); } $final .= implode(' ', $words) . "\n"; } // Return the final string return $final; } ?> Examples of output for the average algorithm: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. (50 characters wide) ---- Server IP: 87.124.64.96 Probable Submitter: 81.159.255.143 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/81236 Del: integrated -- https://master.php.net/note/delete/81236/integrated Del: useless -- https://master.php.net/note/delete/81236/useless Del: bad code -- https://master.php.net/note/delete/81236/bad+code Del: spam -- https://master.php.net/note/delete/81236/spam Del: non-english -- https://master.php.net/note/delete/81236/non-english Del: in docs -- https://master.php.net/note/delete/81236/in+docs Del: other reasons-- https://master.php.net/note/delete/81236 Reject -- https://master.php.net/note/reject/81236 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136283) next »