Same name and namespace in other branches
  1. 4.7.x modules/node.module \node_teaser()
  2. 5.x modules/node/node.module \node_teaser()
  3. 6.x modules/node/node.module \node_teaser()

Automatically generate a teaser for a node body in a given format.

2 calls to node_teaser()
node_preview in modules/node.module
Generate a node preview, including a form for further edits.
node_validate in modules/node.module
Perform validation checks on the given node.

File

modules/node.module, line 157
The core that allows content to be submitted to the site.

Code

function node_teaser($body, $format = NULL) {
  $size = variable_get('teaser_length', 600);

  // find where the delimiter is in the body
  $delimiter = strpos($body, '<!--break-->');

  // If the size is zero, and there is no delimiter, the entire body is the teaser.
  if ($size == 0 && $delimiter == 0) {
    return $body;
  }

  // We check for the presence of the PHP evaluator filter in the current
  // format. If the body contains PHP code, we do not split it up to prevent
  // parse errors.
  if (isset($format)) {
    $filters = filter_list_format($format);
    if (isset($filters['filter/1']) && strpos($body, '<?') !== false) {
      return $body;
    }
  }

  // If a valid delimiter has been specified, use it to chop of the teaser.
  if ($delimiter > 0) {
    return substr($body, 0, $delimiter);
  }

  // If we have a short body, the entire body is the teaser.
  if (strlen($body) < $size) {
    return $body;
  }

  // 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.
  // When even the first paragraph is too long, we try to split at the end of
  // the next sentence.
  $breakpoints = array(
    '</p>' => 4,
    '<br />' => 0,
    '<br>' => 0,
    "\n" => 0,
    '. ' => 1,
    '! ' => 1,
    '? ' => 1,
    '。' => 1,
    '؟ ' => 1,
  );
  foreach ($breakpoints as $point => $charnum) {
    if ($length = strpos($body, $point, $size)) {
      return substr($body, 0, $length + $charnum);
    }
  }

  // If all else fails, we simply truncate the string.
  return truncate_utf8($body, $size);
}