Skip to content

Instantly share code, notes, and snippets.

@jeremyboggs
Created May 16, 2011 13:17
Show Gist options
  • Save jeremyboggs/974424 to your computer and use it in GitHub Desktop.
Save jeremyboggs/974424 to your computer and use it in GitHub Desktop.
Retrieve a substring of the text by limiting the word count. Taken from Omeka and slightly modified.
<?php
/**
* Retrieve a substring of the text by limiting the word count.
* Note: it strips the HTML tags from the text before getting the snippet
*
* @param string $text The text string to snip.
* @param integer $maxWords The number of words to return from the text.
* @param string $endString Optional string to use on the end of the
* returned snippet. Uses '&hellip;.' by default.
* @return string
**/
function snippet_by_word_count($text, $maxWords = 20, $endString = '&hellip;.')
{
// strip html tags from the text
$text = strip_formatting($text);
if ($maxWords > 0) {
$textArray = explode(' ', $text);
if (count($textArray) > $maxWords) {
$text = implode(' ', array_slice($textArray, 0, $maxWords)) . $endString;
}
} else {
return '';
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment