icodemonkey

Trim code snippet

Copied from: http://drupal.org/node/46391 Latest update of this script inversed - March 31, 2008 - 17:44 I was using this and having some problems. Short amounts of text were resulting in an extra greater than sign and strong tags were breaking the results. This is Fat Matt's updated code which works great for me now.

<?php
function drupalicious_summarise($paragraph$limit) {

    
// STEP 1: trim the text down to the nearest sentence determined by $limit
    // NOTE: This step will only end text if the End Punctuation is met by a blank space, NOT a new or close paragraph tag ("<p>" or "</p>").
   
    
$textfield strtok($paragraph" ");
    while(
$textfield) {
   
        
$text .= " " $textfield;
        
$words++;
       
        
// Check if the limit has been met. ALSO check if the last character in the token word is an End Punctuation.  If not, move on to the next word
        
if (($words >= $limit) && ((substr($textfield, -1) == "!")||(substr($textfield, -1) == ".")||(substr($textfield, -1) == ":")||(substr($textfield, -1) == "?"))) {             break;
        } else {
            
$textfield strtok(" ");
        };
    };
   
    
// STEP 2: Once text has been trimmed to the word limit (to the nearest sentence), check for any HTML closing tags (">")
    
$tags 0;
    
$textfield strtok($text">");
    while(
$textfield) {
       
        
// Only re-add the closing tag if there is a tag to close (text might of been trimmed in first step where closing tag would of been excluded)
        
if ((substr($textfield, -1,1) == "!") || (substr($textfield, -1,1) == ".") || (substr($textfield, -1,1) == ":") || (substr($textfield, -1,1) == "?")) {
        
//if ((substr($textfield, -1,1) != "/") || (substr($textfield, -2,1) != "/")) {
            
$revText .= $textfield;
        } else {
            
$revText .= $textfield ">"// . '---' . $tags . '---';
        
};
        
$tags++;
       
        
// Count the number of words when each closing tag is met to ensure the word count isn't under the limit ($limit)
        // Note: Using a different word count method in order not to break the current String position in the "strtok()" method
        
$wordCount explode(" "$revText);
        
$words count($wordCount);
       
        
// If word count is over, check to see if the tag being closed in a paragraph.  If so, return the trim text
        
if ($words >=$limit) {
            if ((
$tags >= 2) && ((substr($textfield, -2,2) == '<p') || (substr($textfield, -3,3) == '</p') )) {
                
$wordCount ""// Dump the array to save memory
                
break;
            } else {
                
$textfield strtok(">");
            };
        } elseif (
substr($textfield, -3,3) == '</p') {   
            
$wordCount ""// Dump the array to save memory
            
break;
        } else {
            
$textfield strtok(">");
        };
    };
   
    if (
substr($revText,-2,2) != "p>") {
        
$revText .= "</p>";
        return 
ltrim($revText);
    } else {
        return 
ltrim($revText);
    };
};
?>