benji87 Posted November 21, 2006 Share Posted November 21, 2006 Hi all ive currently got some news displaying which i have limited to 75 characters using the substr() function so the user can click and read more. Im just wondering if there is a way to get the script to not chop off the end of words. As every bit of news will be different lengths there is no garuntee that it will show whole words when it comes to the end of the string of 75 charcs.For example if i have news with the first sentance being displayed i want it to show like this:This is the first sentance of this news article.and not this:This is the first sentance of this neCan anyone help? Quote Link to comment Share on other sites More sharing options...
taith Posted November 21, 2006 Share Posted November 21, 2006 that should do :-)[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;}?>[/code] Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 21, 2006 Share Posted November 21, 2006 Take a look at [url=http://www.php.net/wordwrap]wordwrap()[/url] and use it together with [url=http://www.php.net/explode]explode()[/url]ex:[code=php:0]$string = 'Hi all ive currently got some news displaying which i have limited to 75 characters using the substr() function so the user can click and read more.';$string = wordwrap($string, 75, "|", 1);list($string) = explode('|', $string);echo $string;[/code] 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.