Generate Excerpt without Breaking a Word

Here’s a little snippet for generating an excerpt without breaking up a word:

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 . ' <a href="' . $uri . '">more</a>';
     return $newString;


Check For an Element on the Page with jQuery

Sometimes I write a function that I only want to apply to certain pages. Say, a form. I may need some javascript to apply to the form when I’m inserting and different javascript to apply when I’m editing. Since I’m reusing the form, I just change the id of the form and use this check:

if ($('#insert-form').length > 0) {
// do stuff that only applies to the insert form