Hello again,
Can anyone give me any pointers on how to make a 'teaser' or 'preview' of an article.
Thanks
Taffd
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 #
# http://www.haneng.com/code/VBScript/CropSentence.txt #
##################################################################
# #
# 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
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.