Created
May 16, 2011 13:17
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 '….' by default. | |
* @return string | |
**/ | |
function snippet_by_word_count($text, $maxWords = 20, $endString = '….') | |
{ | |
// 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