Fair enough, here is the same function with comments this time 
/**
* Create a summary of a given input
* @param string $text The text to summarize
* @param string $length The maximum number of characters in the summary
* @param string $append The text to append to the summary
* @param bool $splitOnWholeWords Whether to split on whole words (take spaces into account) or just take $length characters
*/
function summarize($text, $length, $append = '...', $splitOnWholeWords = true)
{
// If the length of text is shorter than the input text nothing needs to be done
// and we can just return the input text
if (strlen($text) <= $length) return $text;
// initialize the $split variable
$split = 0;
if ($splitOnWholeWords) // If we want to split on whole words ...
{
// initialze variables
$i = 0; $lplus1 = $length + 1;
// Keep scanning the string for spaces until we end up on a position
// in the string that is larger than $length
while (($i = strpos($text, ' ', $i + 1)) < $lplus1)
{
if ($i === false) break; // There are no more spaces in the string
$split = $i; // Split the string after $split characters
}
}
else // ... otherwise, if we don't want to split on whole words
$split = $length;
// Take the found portion of the string and append $append
return substr($text, 0, $split).$append;
}
And here is an example of how it works:
echo summarize('The quick brown fox jumps over the lazy dog', 37);
String to summarize:
The quick brown fox jumps over the lazy dog
Maximum number of characters to take from the string: 37
Find a space. The first space is found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
We don’t have 37 characters yet, so search for the text space, which can be found here:
The quick brown fox jumps over the lazy dog
^
Now we have more than 37 characters, so we stop searching and revert to the last known position of a space that resulted in less than 37 characters.
The quick brown fox jumps over the lazy dog
^
Now append the “…” to the end and the final result will be
The quick brown fox jumps over the...
(the example does not 100% accurately describe how the process works, but is a simplification to make it more clear).
Makes sense?