Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. /**
  2. * This function truncates a string to the desired character limit
  3. * without cutting words.
  4. *
  5. * Adds ellipsis at the end
  6. *
  7. * @param string $str The string to be truncated
  8. * @param int $limit Number of characters to display
  9. * @return string $str Return the truncated string
  10. */
  11. function strcut( $str = '', $limit = 250 ) {
  12.  
  13. if( strlen( $str ) > $limit ) :
  14.  
  15. $str = substr( $str, 0, $limit);
  16. $str = substr( $str, 0, strrpos( $str, ' ' )) . '...';
  17. $str = strip_tags( $str );
  18.  
  19. endif;
  20.  
  21. $str = str_replace( "\n", ' ', $str );
  22.  
  23. return $str;
  24.  
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement