feri_soft Posted January 6, 2007 Share Posted January 6, 2007 How to break up a string for example $row['asdasdasd']when it has more than 300 chars show the first 300 and display more... at the end.if(strlen($text) > 300){ $text }I have that for now.... from here??? Quote Link to comment Share on other sites More sharing options...
taith Posted January 6, 2007 Share Posted January 6, 2007 <?function filter_wordlimit($string, $length=50, $ellipsis='...'){ return count($words = preg_split('/\s+/', ltrim($string), $length + 1)) > $length ? rtrim(substr($string, 0, strlen($string) - strlen(end($words)))) . $ellipsis : $string;}?> Quote Link to comment Share on other sites More sharing options...
feri_soft Posted January 6, 2007 Author Share Posted January 6, 2007 Nice, but can i have a little explanation on the code, please? Quote Link to comment Share on other sites More sharing options...
taith Posted January 6, 2007 Share Posted January 6, 2007 [code]<?function filter_wordlimit($string, $length=50, $ellipsis='...'){ return count($words = preg_split('/\s+/', ltrim($string), $length + 1)) > $length ? rtrim(substr($string, 0, strlen($string) - strlen(end($words)))) . $ellipsis : $string;}echo filter_wordlimit('I can put a really long paragraph in here if i want to, and it will trim it down to 20 words long','20');?>[/code] Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted January 6, 2007 Share Posted January 6, 2007 feri_soft doesnt want to know how to use the function, but how the code works. Quote Link to comment Share on other sites More sharing options...
taith Posted January 6, 2007 Share Posted January 6, 2007 oh... ok :-) basically... it counts how many words are in, the string, if its not greater then the $length, it returns it as is, if it is greater, it cuts off after the last allowed word and adds "..." and returns that. Quote Link to comment Share on other sites More sharing options...
feri_soft Posted January 6, 2007 Author Share Posted January 6, 2007 It is working now, but actually i wanted something to count the chars not the individual words, something with strlen maybe... Quote Link to comment Share on other sites More sharing options...
taith Posted January 6, 2007 Share Posted January 6, 2007 [code]<?function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...';}?>[/code]there ya go... that one counts chars, not words :-) 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.