Created
March 22, 2015 16:51
-
-
Save AmrMekkawy/f1758269f2cf88445eca to your computer and use it in GitHub Desktop.
Get specific number of words from a string.
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
/** | |
* returns a specific number of words | |
* | |
* @param string $string the whole string | |
* @param integer $num_words number of words to return from the whole string | |
* @return string returned string | |
*/ | |
if (!function_exists("brief_text")) { | |
function brief_text($string, $num_words = 50) { | |
$string = trim(preg_replace('/\s+/', ' ', $string)); | |
$words = explode(" ", $string); | |
$required_words = array_slice($words, 0, $num_words); // get sepecific number of elements from the array | |
return implode(" ", $required_words); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment