English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Does anyone know how to make it so that when a peice of text or numbers reaches over a certain number the script cuts the wrod off and adds "..." on the end. Like yahoo does sometimes.

E.g. Say the number to cut off at is 39. This would happen:
"I went to the park today and it suddenl..."

2006-11-27 09:02:04 · 2 answers · asked by peter s 1 in Computers & Internet Programming & Design

2 answers

I don't actually know php, but I am familiar with several other languages (VB.Net, C#, Java). The simplest way would be to test for the length of the string and see if it is over 39 characters. If it is, then create a substring using only the first 39 characters and dump the rest.

This example may help, but since I don't know php and I don't have it installed on my computer, I'm not completely sure that the syntax is correct. It should, however, look something like this:

function sub($string){
$Length = strlen($string);

if($Length > 39)
$string = substr($string, 0, 39) . "...";

return($string);
}
?>

2006-11-27 09:13:36 · answer #1 · answered by Jackson 2 · 0 0

function summary($content, $limit){

$content = explode(' ',$content);
for($i=0; $i<$limit; $i++){
$summary[$i] = $content[$i];
}
$summary = implode(' ', $summary).'..';

return $summary;
}

$content = 'This is some demo content. You can put all you want in here, but only the first 3 words will be returned.';

echo summary($content,3); // assign the content and limit
?>


REturns:
This is some..

Modify to how much you want to show.

2006-11-27 09:13:55 · answer #2 · answered by arus.geo 7 · 0 0

fedest.com, questions and answers