Posted By

gfazioli on 09/21/08


Tagged

php


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

gfazioli
basicmagic
JimiJay


Very short snippet: PHP word cut


 / Published in: PHP
 

URL: http://www.undolog.com/2008/09/21/very-short-snippet-php-word-cut/

Word Cut Function

  1. /**
  2.  * String word cut
  3.  *
  4.  * @private
  5.  */
  6. function _wpmutility_wordCut($content, $limit){
  7. $content = explode(' ',$content);
  8. for($i=0; $i<$limit; $i++) $summary[$i] = $content[$i];
  9. $summary = implode(' ', $summary).'...';
  10. return $summary;
  11. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: torkil on December 13, 2008

In my opinion this is a better solution, both because you avoid the loop but it also takes into consideration that the content might be shorter than the limit:

$contentTemp = explode(' ', $content); if (count($contentTemp) > $limit) { $content = implode(' ', array_slice($contentTemp, 0, $limit)) . '...'; } return $content;

Posted By: rheel on March 22, 2011

$cut=explode("\n",wordwrap($content,250)); return $cut[0].(count($cut)>1 ? '...' : '');

You need to login to post a comment.