Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Words limit
<?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
You can’t perform that action at this time.