Skip to content

Instantly share code, notes, and snippets.

@andylolz
Created November 28, 2012 23:44
Show Gist options
  • Select an option

  • Save andylolz/4165606 to your computer and use it in GitHub Desktop.

Select an option

Save andylolz/4165606 to your computer and use it in GitHub Desktop.
Typeset some content for a textbox; truncate it to fit.
<?php
/**
* Preliminary work on the typesetter.
*
* $input_str - the input
* $width - the width of the text area in characters
* $height - the height of the text area in characters
* $overflow_str - the string appended to the text area, in the event of an overflow
*
* returns the typeset contents of the text area
*/
function typeset($input_str, $width, $height, $overflow_str = "") {
// bit of error checking
if($width < 1 || $height < 1) {
throw new Exception("The width and height must be greater than zero");
}
// Limit the number of consecutive carriage returns to two
$input_str = preg_replace("/(\n{2}\n+)/", "\n\n", $input_str);
// wordwrap() doesn't handle carriage returns, so let's first
// split into an array on those
$lines = explode("\n", $input_str);
// build up an array of wordwrapped strings
foreach($lines as $line) {
$wrapped_arr[] = wordwrap($line, $width, "\n", true);
}
// turn our array of wordwrapped strings into a single string
$wrapped = implode("\n", $wrapped_arr);
// if it fits in the box, we're done!
if(substr_count($wrapped, "\n") <= $height) {
return $wrapped;
}
// wordwrap our overflow string (if necessary).
// We want to wordwrap it backwards, so we reverse,
// wordwrap, then reverse again.
$overflow_arr = explode("\n", strrev(wordwrap(strrev($overflow_str), $width, "\n", true)));
// bit more error checking
if(count($overflow_arr) > $height) {
throw new Exception("The overflow string doesn't even fit in the space!");
}
// Turn the full wordwrapped string into an array
$full_wrapped_arr = explode("\n", $wrapped);
// the number of normal wordwrapped lines
$num_normal_lines = $height - count($overflow_arr);
// add these normal lines to the truncated output array
$trunc_arr = array_slice($full_wrapped_arr, 0, $num_normal_lines);
// add the partial line, plus the first line of the
// overflow string
$partial = explode("\n", wordwrap($full_wrapped_arr[$num_normal_lines], $width - strlen($overflow_arr[0]), "\n", true));
array_push($trunc_arr, $partial[0] . $overflow_arr[0]);
// add the rest of the overflow string
if(count($overflow_arr) > 1) {
$trunc_arr = array_merge($trunc_arr, array_slice($overflow_arr, 1));
}
// the output string
return implode("\n", $trunc_arr);
}
?>
<html>
<head>
<style>
* { font-family: "Courier New"; }
</style>
</head>
<body>
<?php
$str = <<<EOF
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. 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. 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.
EOF;
// 60 characters wide, 20 characters tall
echo nl2br(typeset($str, 60, 20, '... [continued on additional sheet]'));
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment