Possible Duplicate:
excerpt in characters

In some of our sites, we to show excerpts of posts (the content administrators don't enter excerpts).
We could use the the_excerpt function, but from what I could see, I can only control the amount of words it extracts, and that's a little too general for us (a word could have 2 letters, or 10 letters...).
So we need a function that takes the number of characters and extracts that amount from the content. But we also don't want the words be cut in the middle. One last need is that the function work with the multy-byte version of the string functions of php (for examplel: use mb_substr instead of substr)

Are there WP built in functions that would do this?


There are no built-in WordPress functions to trim strings by character count.

* Get a limited part of the content - sans html tags and shortcodes -  
* according to the amount written in $limit. Make sure words aren't cut in the middle  
* @param int $limit - number of characters  
* @return string - the shortened content   
  function mop_the_short_content($limit) {  
     $content = get_the_content();  
     /* sometimes there are <p> tags that separate the words, and when the tags are removed,   
     * words from adjoining paragraphs stick together.    
     * so replace the end <p> tags with space, to ensure unstickinees of words */  
       $content = str_replace('</p>', ' ', $content);  
   $content = strip_tags($content);  
   $content = strip_shortcodes($content);  
   $ret = $content; /* if the limit is more than the length, this will be returned */  
   if (mb_strlen($content) >= $limit) {  
      $ret = mb_substr($content, 0, $limit);  
      // make sure not to cut the words in the middle:  
      // 1. first check if the substring already ends with a space  
      if (mb_substr($ret, -1) !== ' ') {  
         // 2. If it doesn't, find the last space before the end of the string  
         $space_pos_in_substr = mb_strrpos($ret, ' ');  
         // 3. then find the next space after the end of the string(using the original string)  
         $space_pos_in_content = mb_strpos($content, ' ', $limit);  
         // 4. now compare the distance of each space position from the limit  
         if ($space_pos_in_content - $limit <= $limit - $space_pos_in_substr) {  
            /* if the closest space is in the original string, take the substring from there*/  
            $ret = mb_substr($content, 0, $space_pos_in_content);  
         } else {  
            // else take the substring from the original string, but with the earlier (space) position   
            $ret = mb_substr($content, 0, $space_pos_in_substr);  
   return $ret . '...';  

Not the answer you're looking for? Browse other questions tagged or ask your own question.