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? Link to comment https://forums.phpfreaks.com/topic/27957-help-with-substr/ 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] Link to comment https://forums.phpfreaks.com/topic/27957-help-with-substr/#findComment-127875 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] Link to comment https://forums.phpfreaks.com/topic/27957-help-with-substr/#findComment-127883 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.