Hello again,
Can anyone give me any pointers on how to make a 'teaser' or 'preview' of an article.

Thanks
Taffd

Dani AI

Generated

A few practical notes that build on ’s word-safe approach and ’s quick substr trick.

Both answers point to the two main choices: server-side crop or client-side tooltip. The common pitfalls to avoid are: leaving HTML tags in the teaser, breaking multibyte text, injecting unescaped user content into an attribute (XSS), and relying on the title attribute for accessibility (mobile and screen readers are inconsistent).

A compact, safer server-side pattern:

  • strip HTML,
  • handle multibyte text,
  • trim to a whole word,
  • escape for HTML attributes.

Example PHP helper (uses mbstring):

function create_teaser($html, $max = 120, $suffix = '...') {
    $text = trim(strip_tags($html));
    if (mb_strlen($text, 'UTF-8') <= $max) return $text;
    $snippet = mb_substr($text, 0, $max + 1, 'UTF-8');
    $lastSpace = mb_strrpos($snippet, ' ', 0, 'UTF-8');
    if ($lastSpace !== false) $snippet = mb_substr($snippet, 0, $lastSpace, 'UTF-8');
    else $snippet = mb_substr($snippet, 0, $max, 'UTF-8');
    return rtrim($snippet) . $suffix;
}

$teaser = create_teaser($message, 100);
echo 'title="' . htmlspecialchars($teaser, ENT_QUOTES, 'UTF-8') . '"';

If you need richer previews or better accessibility, avoid title and use data- attributes with an ARIA-friendly tooltip component (show on hover and focus). For very long previews, lazy-load content via AJAX on hover to keep pages light. For large lists, precompute and store teasers in the DB for performance.

Quick checklist: always strip tags, handle multibyte, escape with ENT_QUOTES, prefer accessible tooltips over long title text, and consider lazy-loading for large content lists.

Recommended Answers

All 2 Replies

Simple.

Take the text of the article as a string, crop the string to a certain size, append "......" to the string and display it.

<?php

/*################################################################
#                                                                #
# You pass the script a string, a length you want the string     #
# to be and the trailing characters, what the function does,     #
# is takes the string, finds the last word that will fit into    #
# the overall length, and return a string that has been cropped. #
# The function makes sure that a word is not cut in half.        #
#                                                                #
##################################################################
#        Written by David Speake - david@evilwarus.com           #
#      Adapted from Oliver Southgate's ASP interpretation        #
#            #
##################################################################
#                                                                #
# Examples:                                                      #
#                                                                #
# $strTemp = "Hello, I am a fish and you are not.";              #
# $strTemp = CropSentence($strTemp, 16, "...");                  #
# //returns "Hello, I am a..."                                   #
#                                                                #
# $strTemp = "Hello, I am a fish and you are not.";              #
# $strTemp = CropSentence($strTemp, 17, "...");                  #
# //returns "Hello, I am a fish..."                              #
#                                                                #
################################################################*/

function CropSentence ($strText, $intLength, $strTrail) 
{
    $wsCount = 0;
    $intTempSize = 0;
    $intTotalLen = 0;
    $intLength = $intLength - strlen($strTrail);
    $strTemp = "";

    if (strlen($strText) > $intLength) {
        $arrTemp = explode(" ", $strText);
        foreach ($arrTemp as $x) {
            if (strlen($strTemp) <= $intLength) $strTemp .= " " . $x;
        }
        $CropSentence = $strTemp . $strTrail;
    } else {
        $CropSentence = $strText;
    }

    return $CropSentence;
}

$strTemp = "Hello, I am a fish and you are not.";
$strTemp = CropSentence($strTemp, 16, "..."); 
print $strTemp;
?>

Thanks FireNet,
As it happens, I found a much simpler way to achieve what I desired, which was to show a teaser when a forum link was rolled over(as here on daniweb).
I used the following code in the title parameter of the topic's (a href) tag, $message being the text I wanted to truncate. It works.

title="<?php echo substr($message, 0, 100); ?>"

I'd like to add a thankyou to all contributors to daniweb. You've helped me enormously with my project, which I hope to complete in the next week or two.

Best Regards,
Taffd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.