Posted By

ratnams on 05/16/11


Tagged

String word


Versions (?)

Cut Word PHP


 / Published in: PHP
 

  1. function word_limiter($str, $limit = 100)
  2. {
  3. if (trim($str) == '')
  4. {
  5. return $str;
  6. }
  7.  
  8. preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
  9.  
  10. if (strlen($str) == strlen($matches[0]))
  11. {
  12. $end_char = '';
  13. }
  14.  
  15. return rtrim($matches[0]);
  16. }
  17.  
  18.  
  19. function excerpt($full_text, $num_word=100, $allowable_tags='')
  20. {
  21. $strip = strip_tags($full_text, $allowable_tags);
  22. if(count($strip) > 0){
  23. $result = word_limiter($strip, $num_word);
  24. }else{
  25. $result = $strip;
  26. }
  27. return $result;
  28. }

Report this snippet  

You need to login to post a comment.