...

View Full Version : Only Display a certain amount of words with a View more button after how?



maxvee8
05-09-2008, 08:10 PM
hi,

I have:

Table : sweets

Fields: sweetname, Description

i want to select the description and the title print them out but only print out say print 15 words from the description and then print a view more button

how is this done? please give a simple example if possible as im not sure

example below

http://img382.imageshack.us/img382/9909/sweetimgkx3.jpg

max

Iszak
05-09-2008, 11:30 PM
You could use


<?php

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
$string = explode(" ", $string);

$i = 0;
while ($i < 15)
{
echo $string[$i]." ";
$i++;
}
// Outputs Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has

?>


or... an alternative method not on words..


<?php

$string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
echo substr($string, 0, 100)."...";
// Outputs Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the ...

?>


I would prefer the second method cause the first method relies on word length and you can get some long words..

Bill Posters
05-09-2008, 11:56 PM
Fwiw, I put together this page a while back to compare two functions for excerpting to specified lengths.

The trimTextChars() function which I'd written allows me to excerpt text to a specific max character length, but without producing partial words at the end.

http://dev.akabillposters.com/php/truncated-text/


// trim to N characters
function trimTextChars($src_text, $char_count) {

$char_count = (isset($char_count)) ? $char_count : 100; # if $char_count is not set, it defaults to 100
$subtxt = substr($src_text,0,$char_count);
$xslength = strlen(strrchr($subtxt,' '));
return substr($subtxt,0,$char_count-$xslength).'&hellip;';

}

maxvee8
05-11-2008, 07:57 PM
i used iszaks 2nd example just for simplicity worked perfect .. thanks max



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum