I'd like to print (in Views - custom/PHP field) random part of body of a node, as a kind of teaser, different with every print.
Any ideas? Thanks! :]
Szy.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityhere you go: :)
$length = 50;
// strip html tags from body
$body = strip_tags($data->body);
// check that the body's actually longer than your snippet length
if (strlen($body) <= $length) {
return $body;
}
else {
// find a random starting point between the beginning and 50 chars from the end
$max = strlen($body) - $length;
$start = rand(0, $max);
// set ellipsis variables
$prefix = $start ? '...' : '';
$suffix = ($start == $max) ? '' : '...';
return $prefix . substr($body, $start, $length) . $suffix;
}