Ok, well first we'll assume that you got the contents from a database or file and put it into a variable. Ex: $contents
$contents = 'Some article or something';
First we'll check to see if the user want's the entire contents. This will be passed by a GET variable.
URL: http://www.com/page.php?all=yes
if (isset($_GET['all']) && $_GET['all'] == 'yes') { //...
If the user DOES want the entire contents we'll just print it.
print($contents);
Else, print the shortened contents with a link to the entire contents. Note: I'll be using a function I created called str_shorten().
} else {
print(str_shorten($contents, 2000));
print(' <a href="?all=yes">View All</a>');
}
So, putting it all together it would look like:
$contents = 'Some article or something'; // Variable with article string or whatever.
if (isset($_GET['all']) && $_GET['all'] == 'yes') { // See if they want the entire contents
print($contents); // Print the entire contents
} else {
print(str_shorten($contents, 2000)); // Shorten the contents if needed and print
print(' <a href="?all=yes">View All</a>'); // Print link to show entire contents
}
And the str_shorten() function. (NOT built into PHP.)
function str_shorten($str, $len, $end='...')
{
if ($len < strlen($str)) {
$cut = substr($str, 0, $len);
if ($str{$len} == ' ')
$short = $cut . $end;
elseif ($str{$len-1} == ' ')
$short = rtrim($cut) . $end;
elseif (($space = strrpos($cut, ' ')) !== false)
$short = substr($cut, 0, $space) . $end;
else
$short = $cut . $end;
} else
$short = $str;
return($short);
}
Let me know if you have questions.