Mutley Posted December 17, 2006 Share Posted December 17, 2006 Is it possible when displaying a field from a database to limit it to the first 15 or so characters? For example, if I had this in my database:"Hello world, what a great day!"It would just display it as:"Hello world..."Thanks. Link to comment https://forums.phpfreaks.com/topic/30984-limit-characterswords-from-database/ Share on other sites More sharing options...
paul2463 Posted December 17, 2006 Share Posted December 17, 2006 write a simple function for display purposes such as [code]<?phpfunction displayText($text){ $addon = "..."; $text1 = substr($text, 0, 11); $returntext = "$text1$addon"; return $returntext;}?>[/code]$text = "Hello World how are you today?";$newtext = displayText($text);echo $newtext ;would display Hello World...hope that helps Link to comment https://forums.phpfreaks.com/topic/30984-limit-characterswords-from-database/#findComment-142985 Share on other sites More sharing options...
Thierry Posted December 17, 2006 Share Posted December 17, 2006 Note that this would also add the "..." to a value that already has less then say 15 characters.Solution:[code]<?phpfunction displayText($text){ $addon = "..."; $text1 = substr($text, 0, 11); if($text <> $text1){ $returntext = "$text1$addon";}else{$returntext = $text;} return $returntext;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/30984-limit-characterswords-from-database/#findComment-143077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.