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??? Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/ 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;}?> Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154169 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? Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154262 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] Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154265 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. Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154268 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. Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154271 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... Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154272 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 :-) Link to comment https://forums.phpfreaks.com/topic/33090-breaking-up-an-article/#findComment-154273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.