<?php

if ( preg_match( '/<img[^>]+data-preview[^>]*>/us', $page['text'], $m ) ) {
    $preview_img = $m[0];
} else {
    $preview_img = '';
}

if ( preg_match( '/^(.*)(<!--[\s]*preview-start[\s]*-->)(.*)(<!--[\s]*preview-end[\s]*-->)(.*)$/us', $page['text'], $m ) ) {
    $preview = $m[3];
} else {
    $preview = preg_replace( "/<h1>(.*?)<\/h1>/u", "", $page['text'] );
    $preview = mb_substr( trim( strip_tags( $preview ) ), 0, 500 );
    $preview = preg_replace( '/\s\S*$/u', ' ...', $preview );
    $preview = "<p>{$preview}</p>";
}

echo "
<div class=teaser>
<h2><a href='{$cms['base_path']}{$page['url']}'>{$page['title']}</a></h2>
{$preview}
</div>
";