Created
March 19, 2014 05:03
-
-
Save ReekenX/9635692 to your computer and use it in GitHub Desktop.
Words limit
This file contains 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 | |
function words_limit( $str, $num, $append_str='' ){ | |
$words = preg_split( '/[\s]+/', $str, -1, PREG_SPLIT_OFFSET_CAPTURE ); | |
if( isset($words[$num][1]) ){ | |
$str = substr( $str, 0, $words[$num][1] ).$append_str; | |
} | |
unset( $words, $num ); | |
return trim( $str ); | |
} | |
echo words_limit($yourString, 50,'...'); | |
// or | |
echo words_limit($yourString, 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment