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 Quote Link to comment 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 :-) Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.