Advertisement
rakeshr

limit words in php

Oct 29th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. <?php
  2.  
  3. function string_limit_words($string, $word_limit) { $words = explode(' ', $string); return implode(' ', array_slice($words, 0, $word_limit)); }
  4.  
  5. ?>
  6.  
  7. Then you can set the values for the string and the word limit whenever you need to.
  8.  
  9. Eg. echo string_limit_words($paragraph, 15);
  10.  
  11.  
  12. also add this css to pre
  13.  
  14. white-space: pre-wrap;       /* css-3 */
  15.  white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
  16.  white-space: -pre-wrap;      /* Opera 4-6 */
  17.  white-space: -o-pre-wrap;    /* Opera 7 */
  18.  word-wrap: break-word;       /* Internet Explorer 5.5+ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement