Jump to content
Sign in to follow this  
Rob

How to generate an Excerpt without breaking up a word

Recommended Posts

Rob    547
Rob

Some of you have asked how to generate an excerpt of a particular length, which often results in a word that's been cut in the middle. This would provide a solution. Use this code:

function excerpt($string='', $maxChar=50, $uri='#') {
	     $length = strlen($string);
	     if ($length < $maxChar) {
	          return $string;
	     }
	     $trimmedString = substr($string, 0, $maxChar);
	     $choppedString = substr($trimmedString, 0, strrpos($trimmedString, strrchr($trimmedString, ' ')));
	     $newString = $choppedString . ' more';
	     return $newString;
	}[/code]

It can be implemented as a shortcode, when placing excerpts in particular places.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
Rob    547
Rob
No prob.

Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×