--- .svn/text-base/node.module.svn-base 2006-12-04 22:55:04.000000000 -0700 +++ node.module 2006-12-08 14:01:09.000000000 -0700 @@ -173,19 +173,41 @@ function node_teaser($body, $format = NU return $body; } + // the teaser length is "the maximum number of characters used in the trimmed version of a post" + $teaser = truncate_utf8($body, $size); + // since we are trying to find the last occurance of something, the reversed teaser is useful + $reversed = strrev($teaser); + // In some cases, no delimiter has been specified (e.g. when posting using // the Blogger API). In this case, we try to split at paragraph boundaries. + $breakpoints = array('

' => 0, '
' => 6, '
' => 4, "\n" => 1); + // strrpos() doesn't accept multi-character needles in PHP4, + // so we use strpos() and reverse the haystack and the needle + foreach ($breakpoints as $point => $offset) { + $length = strpos($reversed, strrev($point)); + if ($length !== false) { + $position = 0 - $length - $offset; + return ($position == 0) ? $teaser : substr($teaser, 0, $position); + } + } + // When even the first paragraph is too long, we try to split at the end of - // the next sentence. - $breakpoints = array('

' => 4, '
' => 0, '
' => 0, "\n" => 0, '. ' => 1, '! ' => 1, '? ' => 1, '。' => 3, '؟ ' => 1); - foreach ($breakpoints as $point => $charnum) { - if ($length = strpos($body, $point, $size)) { - return substr($body, 0, $length + $charnum); + // the last full sentence. + $breakpoints = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1); + $shortest_length = false; + foreach ($breakpoints as $point => $offset) { + $length = strpos($reversed, strrev($point)); + if ($length !== false and ($shortest_length === false or $length < $shortest_length)) { + $position = 0 - $length - $offset; + $shortest_length = $length; } } + if ($shortest_length !== false) { + return ($position == 0) ? $teaser : substr($teaser, 0, $position); + } - // If all else fails, we simply truncate the string. - return truncate_utf8($body, $size); + // If all else fails, we simply return the truncated string. + return $teaser; } /**