Jump to content

limiting characters and words from a string


real_illusions

Recommended Posts

Hi all,I would like to limit the number of characters from a string that is displayed on the page.I am aware of substr() to limit characters, but, that will cut a word in half if the limit is in the middle of the word. Is there a way of utilising the substr() so it doesn't do this? Or can the word limit be done via MySQL??Thanks.

Link to comment
Share on other sites

You could explode() a string by space, get the first N members and implode them. If there are less then or equal, they'll all be imploded, regardless of how long each word is. And if there are more words, the extra would be cut off.This technique only works for limiting words though, not characters.Actually, that's off from what you want... hmm...One approach in general (no implementation) would be to get the substring before the limit, and the substring after the limit. From the substring after the limit, get the substring up to the first space, and append it to the substring within the limit and use that.

Link to comment
Share on other sites

Here's a little function that doesn't take word boundaries into effect, but will add an ellipsis:

function str_ellipsis($str, $len){  if (strlen($str) <= $len)	return $str;  else	return substr($str, $len - 3) . '...';}

You would call that like this to wrap your string at 80 characters:str_ellipsis($str, 80);You can extend that to support word boundaries if you want, you could explode on spaces or use strpos to figure out where spaces are, use the wordwrap function, etc.

Link to comment
Share on other sites

I rather fancy the explode technique, myself. The while conditional looks kludgy, but it works.

function str_ellipsis($str, $len) {	$len -= 3;	$arr = explode(' ', $str);	$str = '';	$i = 0;	while (strlen($str) + strlen($arr[$i]) < $len) {		$str .= $arr[$i] . ' ';		$i++;	}	return substr($str, 0, -1) . '...';}

Link to comment
Share on other sites

Even better:

function str_digest($str, $len) {   return strlen($str) <= $len ? $str : substr($str = substr($str, 0, $len - 3), 0, strrpos($str, ' ')) . '...';}

I usually don't write such terse code, but what the heck?EDIT: took into account strlen($str) <= $len after looking again at JSG's version.

Link to comment
Share on other sites

Thanks for your help.Found quite a nice short snippet of code which seems to do the trick utilising the explode() function.

function limit_words($newsitem, $word_limit){$words = explode(" ",$newsitem);return implode(" ",array_splice($words,0,$word_limit));}$content = htmlspecialchars_decode($info['content']);echo limit_words($newsitem,35);

I think the str_ellipsis was the one PHP function I thought existed about adding a '...' at the end. But the shortened content is only been displayed once, and only 1 bit of text is shortened. But still worthy of trying to remember for echoing out a large number of statements. (but it obviously doesn't exist like a str_ellipsis() type function thingy...i think?)

Link to comment
Share on other sites

Thanks for your help.Found quite a nice short snippet of code which seems to do the trick utilising the explode() function.
function limit_words($newsitem, $word_limit){$words = explode(" ",$newsitem);return implode(" ",array_splice($words,0,$word_limit));}$content = htmlspecialchars_decode($info['content']);echo limit_words($newsitem,35);

I think the str_ellipsis was the one PHP function I thought existed about adding a '...' at the end. But the shortened content is only been displayed once, and only 1 bit of text is shortened. But still worthy of trying to remember for echoing out a large number of statements. (but it obviously doesn't exist like a str_ellipsis() type function thingy...i think?)

not natively, not that I know of. I made one of my own ellipses generators too. Pretty much the same as JSG's.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...