Skip to content

Instantly share code, notes, and snippets.

@ReekenX
Created March 19, 2014 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ReekenX/9635692 to your computer and use it in GitHub Desktop.
Save ReekenX/9635692 to your computer and use it in GitHub Desktop.
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