jfs0479 Posted April 20, 2007 Share Posted April 20, 2007 Hi Guys, Just a quick query, please could someone instruct me as i am new to php coding how to limit the number of charcters when displaying something and having the ending as "..." For example displaying news articles and then a link saying Click Here For More Thanks James Link to comment https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/ Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 here ya go :-) <? function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...'; } echo filter_charlimit("i'm way to long to show the whole string in a wee tiny little textbox",20); ?> basically... if $string is longer then $length, it cuts off the first $length characers and adds '...' if not, it returns the string untouched :-) Link to comment https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/#findComment-234005 Share on other sites More sharing options...
Canman2005 Posted April 20, 2007 Share Posted April 20, 2007 Use the following function <?php function limitString($string, $maxlength) { if (strlen($string) > $maxlength) { print nl2br(substr($string, 0, $maxlength)."..."); } else { print $string; } } ?> and then on the text you want to limit, use <?php limitString($text, 150); ?> $text being your variable and the 150 marks the limit for the characters Hope this helps Ed Link to comment https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/#findComment-234018 Share on other sites More sharing options...
jfs0479 Posted April 20, 2007 Author Share Posted April 20, 2007 Thank you ever so much for your replies! Link to comment https://forums.phpfreaks.com/topic/47885-limiting-no-charcters/#findComment-234069 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.